2017-04-24 52 views
0

我試圖從同一個事務(從TestService服務實現的savePerson()方法內)插入兩條記錄(Person)。回滾的Spring事務不會將DB恢復到以前的狀態

另外我在PERSON表的first_name列上創建了唯一的鍵索引 - 這樣當我嘗試插入兩個具有相同名字的人時,我可以獲得重複鍵錯誤和測試事務回滾。

執行savePerson()方法時,JTA會因重複鍵啓動回滾,但它也不會在數據庫中啓動回滾。我在這裏錯過了什麼?

在MySQL通用日誌中,沒有sql「START TRANSACTION」,後跟「ROLLBACK」/「COMMIT」。當啓動WildFly服務器時,我可以在日誌sql語句中看到「SET autocommit = 1」。 @Transactional註釋是否應該照顧這一點 - 在數據庫級別處理事務?

我使用spring-boot,hibernate和MySQL jdbc。

的console.log

15:00:54,060 DEBUG default task-12 jta.JtaTransactionManager:367 - Creating new transaction with name [com.example.test.TestServiceImpl.savePerson]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception 
15:00:54,066 DEBUG default task-12 support.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'transactionManager' 
15:00:54,067 DEBUG default task-12 jta.JtaTransactionManager:476 - Participating in existing transaction 
15:00:54,083 INFO default task-12 stdout:71 - Hibernate: insert into person (age, first_name, last_name) values (?, ?, ?) 
15:00:54,125 DEBUG default task-12 jta.JtaTransactionManager:476 - Participating in existing transaction 
15:00:54,126 INFO default task-12 stdout:71 - Hibernate: insert into person (age, first_name, last_name) values (?, ?, ?) 
15:00:54,134 WARN default task-12 spi.SqlExceptionHelper:129 - SQL Error: 1062, SQLState: 23000 
15:00:54,135 ERROR default task-12 spi.SqlExceptionHelper:131 - Duplicate entry 'John' for key 'idx_first_name_unique' 
15:00:54,137 DEBUG default task-12 jta.JtaTransactionManager:858 - Participating transaction failed - marking existing transaction as rollback-only 
15:00:54,137 DEBUG default task-12 jta.JtaTransactionManager:1074 - Setting JTA transaction rollback-only 
15:00:54,138 DEBUG default task-12 support.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'entityManagerFactory' 
15:00:54,140 DEBUG default task-12 jta.JtaTransactionManager:851 - Initiating transaction rollback 

我使用MySQL的InnoDB表。這裏的服務器日誌(無COMMIT,沒有設置自動提交= 0,沒有ROLLBACK ???)標識符爲#22相同的連接上:

  22 Connect [email protected] as anonymous on test 
      22 Query /* mysql-connector-java-6.0.6 (Revision: 3dab84f4d9bede3cdd14d57b99e9e98a02a5b97d) */SELECT @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS tx_isolation, @@wait_timeout AS wait_timeout 
      22 Query SELECT @@session.autocommit 
      22 Query SET NAMES utf8mb4 
      22 Query SET character_set_results = NULL 
      22 Query SET collation_connection = utf8_general_ci 
      22 Query SET autocommit=1 
      22 Query SET sql_mode='NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES' 
      22 Query SET autocommit=1 
170424 15:02:07 22 Query SHOW FULL TABLES FROM `test` LIKE '%' 
      22 Query SHOW FULL TABLES FROM `test` LIKE '%' 
      22 Query SHOW FULL COLUMNS FROM `person` FROM `test` LIKE '%' 
      22 Query SHOW INDEX FROM `person` FROM `test` 
170424 15:02:24 22 Query SELECT 1 
      22 Query insert into person (age, first_name, last_name) values (40, 'John', 'Smith') 
      22 Query insert into person (age, first_name, last_name) values (40, 'John', 'Smith') 

相反,應該是這樣的:

set autocommit = 0 
insert into ... 
insert into ... 
rollback 

TestServiceImpl.java

package com.example.test; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

import com.example.test.Person; 

@Service 
public class TestServiceImpl implements TestService { 

    @Autowired 
    private TestRepository testRepository; 

    @Override 
    @Transactional(rollbackFor = Exception.class) 
    public void savePerson() { 
     Person p1 = new Person("John", "Smith", 40); 
     Person p2 = new Person("John", "Smith", 40); 

     testRepository.save(p1); 
     testRepository.save(p2); 
    } 

} 

TestService.java

package com.example.test; 

public interface TestService { 
    public void savePerson(); 
} 

TestRepository.java:

package com.example.test; 

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

@Repository 
public interface TestRepository extends JpaRepository<Person, Integer>{ 

} 

Application.java:

package com.example.test; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@SpringBootApplication 
@EnableTransactionManagement 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(
      SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) { 

     SpringApplication.run(Application.class, args); 
    } 
} 

Person.java

package com.example.test; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.Index; 

@Entity 
@Table(name = "PERSON", indexes = { @Index(name = "idx_first_name_unique", columnList = "first_name", unique = true), }) 
public class Person { 

    @Id 
    @GeneratedValue 
    @Column(name = "id") 
    private Integer id; 

    @Column(name = "first_name") 
    private String firstName; 

    @Column(name = "last_name") 
    private String lastName; 

    @Column(name = "age") 
    private Integer age; 

    public Person() { 
    } 

    public Person(String firstname, String lastname, Integer age) { 
     this.firstName = firstname; 
     this.lastName = lastname; 
     this.age = age; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public Integer getAge() { 
     return age; 
    } 

    public void setAge(Integer age) { 
     this.age = age; 
    } 

    @Override 
    public String toString() { 
     return "Person [firstName=" + firstName + ", lastName=" + lastName 
       + ", age=" + age + "]"; 
    } 

} 

POM依賴與版本:

<dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
      <version>1.5.2.RELEASE</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-log4j2</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>com.vaadin</groupId> 
      <artifactId>vaadin-spring-boot-starter</artifactId> 
      <version>2.0.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <version>1.5.2.RELEASE</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.1-api</artifactId> 
      <version>1.0.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>5.2.10.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>5.2.10.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>6.0.6</version> 
     </dependency> 
</dependencies> 

application.properties:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test 
spring.datasource.username=test 
spring.datasource.password=test 
spring.datasource.driverClassName=com.mysql.jdbc.Driver 

spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy 
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57InnoDBDialect 
+0

的API調用JDBC連接'setAutoCommit(false)'按照它應該開始的事務。你沒有看到'START TRANSACTION'的原因是因爲JDBC驅動程序將它實現爲'SET autocommit = 0',遺留了與舊版MySQL版本的向後兼容性https://github.com/mysql/mysql-connector-j /blob/6.0.6/src/main/java/com/mysql/cj/jdbc/ConnectionImpl.java#L3773 – coladict

+0

另外,在MySQL中,它關係到你用於表的什麼存儲引擎。只有InnoDB和NDB支持交易。 https://dev.mysql.com/doc/refman/5.5/en/storage-engines.html – coladict

+0

感謝您澄清。但是,在啓動WildFly時,我仍然只能在MySQL服務器日誌中看到「SET autocommit = 1」。它不應該是「SET autocommit = 0」嗎?無論如何,交易不會在數據庫中回滾。我可以看到與自動提交連接時發出的插入設置爲true,這意味着它們每個都作爲單個事務執行。我只是不知道如何在數據庫級別進行交易。 –

回答

0

挖了一段時間後,我找到了!最後。 :)

1)爲了使標準DB事務工作(BEGIN/COMMIT/ROLLBACK),應該禁用JTA。使用spring-boot可以在application.properties中完成。

application.properties:

spring.jta.enabled = false 

2)當使用JTA應啓用XA兩階段提交協議爲特定的數據庫。由於部署的應用程序在Wildfly AS上使用JTA TransactionManager實現,因此應在此處配置所有數據源。而在application.properties中,僅僅使用一些JNDI名稱來引用數據源就足夠了。

Wildfly獨立-demo.xml:

... 
      <datasources> 
       ... 
       <xa-datasource jndi-name="java:jboss/datasources/test" pool-name="TestXADS" enabled="true"> 
        <xa-datasource-property name="url"> 
         jdbc:mysql://127.0.0.1:3306/test 
        </xa-datasource-property> 
        <driver>mysql</driver> 
        <xa-pool> 
         <min-pool-size>10</min-pool-size> 
         <max-pool-size>20</max-pool-size> 
         <prefill>true</prefill> 
        </xa-pool> 
        <security> 
         <user-name>test</user-name> 
         <password>test</password> 
        </security> 
       </xa-datasource> 
       <drivers> 
        ... 
        <driver name="mysql" module="com.mysql"> 
         <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> 
        </driver> 
       </drivers> 
      </datasources> 
... 

模塊/ COM/MySQL的/主/ module.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<module xmlns="urn:jboss:module:1.0" name="com.mysql"> 
    <resources> 
     <resource-root path="mysql-connector-java-5.1.41-bin.jar"/> 
    </resources> 
    <dependencies> 
     <module name="javax.api"/> 
     <module name="javax.transaction.api"/> 
    </dependencies> 
</module> 

application.properties:

spring.datasource.jndi-name=java:jboss/datasources/test 
相關問題