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 [[email protected]]
[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 [[email protected]] for key [[email protected]] from thread [main]
[13 Jul 2012 11:29:13,738] DEBUG TransactionSynchronizationManager | Removed value [[email protected]] for key [[email protected]b] from thread [main]
[13 Jul 2012 11:29:13,738] DEBUG HibernateTransactionManager | Closing Hibernate Session [[email protected]] 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
然而,當我從我的數據庫中刪除,更改是持久的,我未來的測試失敗,因爲表中缺少一個對象。我錯過了什麼嗎?任何方向將不勝感激。提前致謝!!
你如何獲得Hibernate會話?您確定您使用的會話是事務回滾的會話嗎? – axtavt 2012-07-13 18:00:32
'Session session = _sessFac.openSession();'然而,我爲每個@Test獲取一個新會話。雖然我認爲這是合法的,因爲類頂部的@Transactional註釋包含事務中的每個方法。 – nook 2012-07-13 18:09:14
你確定hsqldb支持事務嗎?並非每個數據庫都支持它們例如,在使用MyISAM存儲引擎表時,MySQL不支持事務,只有InnoDB和BDB。檢查這個hsql文檔。 – Matt 2012-07-13 18:00:55