ssm框架中的spring事务回滚问题

nkcskrwz  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(248)

软件版本:jdk 8 spring 4.0 mybatis-3.2.7.jar mybatis-spring-1.2.2.jar ojdbc6.jar oracle 11g applicationcontext.xml:

<context:property-placeholder location="classpath:jdbc.properties" />
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    id="dataSource">
    <property name="url" value="${jdbc.url}"></property>
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="appTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="appTransactionManager" />

junit代码:

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "appTransactionManager", defaultRollback = true)
@Transactional(rollbackFor = Exception.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class GenerateJylshTest {
    @Test
    @Rollback(true)
    public void generateJylshTest() {

        String jyjgbh = "4201000000";
        GenerateJylsh generator = new GenerateJylsh();
        int i;
        for(i = 0;i < 3;i++) {
            String jylshString = generator.generateJylsh(jyjgbh);
            System.out.println(jylshString);
        }
        int k = 1/0;
    }
}

控制台结果:

Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
JDBC Connection [oracle.jdbc.driver.T4CConnection@172b013] will be managed by Spring
==>  Preparing: SELECT * FROM BIZ_VEH_IS_TEMP_FLOWNUM t WHERE t.JCZ = ? AND t.JCXDM = ? 
==> Parameters: 4201000000(String), 42010000001(String)
<==    Columns: JCZ, JCXDM, LSH
<==        Row: 4201000000, 42010000001, 01000019110800027
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306] from current transaction
==>  Preparing: UPDATE BIZ_VEH_IS_TEMP_FLOWNUM SET LSH = ? WHERE JCZ = ? AND JCXDM = ? 
==> Parameters: 01000019110800028(String), 4201000000(String), 42010000001(String)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
01000019110800028
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306] from current transaction
==>  Preparing: SELECT * FROM BIZ_VEH_IS_TEMP_FLOWNUM t WHERE t.JCZ = ? AND t.JCXDM = ? 
==> Parameters: 4201000000(String), 42010000001(String)
<==    Columns: JCZ, JCXDM, LSH
<==        Row: 4201000000, 42010000001, 01000019110800028
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306] from current transaction
==>  Preparing: UPDATE BIZ_VEH_IS_TEMP_FLOWNUM SET LSH = ? WHERE JCZ = ? AND JCXDM = ? 
==> Parameters: 01000019110800029(String), 4201000000(String), 42010000001(String)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
01000019110800029
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306] from current transaction
==>  Preparing: SELECT * FROM BIZ_VEH_IS_TEMP_FLOWNUM t WHERE t.JCZ = ? AND t.JCXDM = ? 
==> Parameters: 4201000000(String), 42010000001(String)
<==    Columns: JCZ, JCXDM, LSH
<==        Row: 4201000000, 42010000001, 01000019110800029
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306] from current transaction
==>  Preparing: UPDATE BIZ_VEH_IS_TEMP_FLOWNUM SET LSH = ? WHERE JCZ = ? AND JCXDM = ? 
==> Parameters: 01000019110800030(String), 4201000000(String), 42010000001(String)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
01000019110800030

Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c7ba306]

2019-11-08 16:31:09 [main] - [ INFO ]  Rolled back transaction after test execution for test context [DefaultTestContext@f316aeb testClass = GenerateJylshTest, testInstance = test.GenerateJylshTest@6aa3a905, testMethod = generateJylshTest@GenerateJylshTest, testException = java.lang.RuntimeException, mergedContextConfiguration = [MergedContextConfiguration@a22cb6a testClass = GenerateJylshTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]

2019-11-08 16:31:09 [Thread-1] - [ INFO ]  Closing org.springframework.context.support.GenericApplicationContext@4667ae56: startup date [Fri Nov 08 16:31:05 CST 2019]; root of context hierarchy

我以前认为这个结果代码

Transaction synchronization rolling back SqlSession 
Transaction synchronization closing SqlSession

意味着事务回滚已经完成,但事实是数据仍然更新到oracle数据库。所以,我想知道为什么。谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题