事务似乎已回滚,但它们不会在数据库中回滚

时间:2012-07-13 17:37:46

标签: java spring hibernate hsqldb

PersistenceHelper-context.xml中

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <!---->
        <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:mydb"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
          depends-on="dataSource" name="_sessFac" >  
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="hibernate.cfg.xml.incDTD"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.shutdown">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

班级标题

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:PersistenceHelper-context.xml"})
@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionManager") 
@Transactional
public class BaseClass extends BaseTestClass{//.....}

然后我想要使用事务的所有类都来自BaseClass,即 public class FooTest extends BaseClass

此外,当我运行我的代码时,我看到以下内容:

HibernateTransactionManager | Initiating transaction rollback
[13 Jul 2012 11:29:13,738] DEBUG  HibernateTransactionManager | Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@107536b]
[13 Jul 2012 11:29:13,738] DEBUG  HibernateTransactionManager | Triggering afterCompletion synchronization
[13 Jul 2012 11:29:13,738] DEBUG  TransactionSynchronizationManager | Clearing transaction synchronization
[13 Jul 2012 11:29:13,738] DEBUG  TransactionSynchronizationManager | Removed value [org.springframework.orm.hibernate3.SessionHolder@17b3cdb] for key [org.hibernate.impl.SessionFactoryImpl@14642ac] from thread [main]
[13 Jul 2012 11:29:13,738] DEBUG  TransactionSynchronizationManager | Removed value [org.springframework.jdbc.datasource.ConnectionHolder@129a6a3] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c506b] from thread [main]
[13 Jul 2012 11:29:13,738] DEBUG  HibernateTransactionManager | Closing Hibernate Session [org.hibernate.impl.SessionImpl@107536b] after transaction
[13 Jul 2012 11:29:13,738] DEBUG  SessionFactoryUtils       | Closing Hibernate Session
[13 Jul 2012 11:29:13,738] INFO   TransactionalTestExecutionListener | Rolled back transaction after test execution for test context

然而,当我从数据库中删除时,更改将保持不变,我的未来测试将失败,因为表中缺少对象。我错过了什么吗?任何方向都会非常感激。在此先感谢!!

2 个答案:

答案 0 :(得分:2)

您应该使用sessionFactory.getCurrentSession()来获取与当前交易相关联的Session

openSession()创建一个新会话,该会话与Spring管理的事务无关。

答案 1 :(得分:0)

我从未使用过DriverManagerDataSource,但我认为它可以将某种“autoCommit”属性默认设置为true。您可以尝试使用像c3p0这样的实际连接池(因为DriverManagerDataSource类不是实际的连接池)。有了它,我对交易没有任何问题。