使用Spring 3.0.5,Hibernate 3和MySQL 5进行事务测试时遇到问题。 在日志中,似乎一切正常,事务回滚,但我将记录插入到数据库中。 我的配置是这样的:
<bean id="hibernateDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/blog" />
<property name="username" value="user" />
<property name="password" value="password" />
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="hibernateDataSource" />
<property name="schemaUpdate" value="true" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>pl.jedenpies.blog.domain.Uzytkownik</value>
</list>
</property>
</bean>
<bean id="uzytkownikDao" class="pl.jedenpies.blog.db.hibernate.dao.HibernateUzytkownikDao">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
@TransactionConfiguration(defaultRollback = true)
public class UzytkownikDaoTest {
private UzytkownikDao uzytkownikDao;
@Test
@BeforeTransaction
public void test1Config() {
Assert.notNull(uzytkownikDao, "UzytkownikDao nie moze byc null");
}
@Test
@Transactional
@Rollback
public void test2Create() {
Uzytkownik u = new Uzytkownik();
u.setEmail("my4@uzytkownik.pl");
u.setHaslo("blablabla");
Assert.isTrue(!u.isIdUstawione());
u = uzytkownikDao.create(u);
Assert.notNull(u, "Uzytkownik nie moze byc null");
Assert.isTrue(u.isIdUstawione());
}
@Resource
public void setUzytkownikDao(UzytkownikDao uzytkownikDao) {
this.uzytkownikDao = uzytkownikDao;
}
}
日志:
DEBUG org.hibernate.event.def.AbstractSaveEventListener - executing identity-insert immediately
DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG org.hibernate.jdbc.AbstractBatcher - insert into uzytkownicy (email, haslo) values (?, ?)
Hibernate: insert into uzytkownicy (email, haslo) values (?, ?)
DEBUG org.hibernate.id.IdentifierGeneratorFactory - Natively generated identity: 43
DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG org.springframework.test.context.transaction.TransactionalTestExecutionListener - Method-level @Rollback(true) overrides default rollback [true] for test context [[TestContext@3dbbd23f testClass = UzytkownikDaoTest, locations = array<String>['classpath:beans.xml'], testInstance = pl.jedenpies.blog.dao.UzytkownikDaoTest@22a010ba, testMethod = test2Create@UzytkownikDaoTest, testException = [null]]]
DEBUG org.springframework.transaction.support.AbstractPlatformTransactionManager - Initiating transaction rollback
DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@303bc1a1]
DEBUG org.hibernate.transaction.JDBCTransaction - rollback
DEBUG org.hibernate.transaction.JDBCTransaction - rolled back JDBC Connection
DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Closing Hibernate Session [org.hibernate.impl.SessionImpl@303bc1a1] after transaction
DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
是的,我搜索了很多这个问题,但没有真正找到解决方案。我不知道出了什么问题。
有什么建议吗?
答案 0 :(得分:4)
您使用的是InnoDB引擎吗? MyISAM是5.5之前的MySQL 5.x的默认引擎形式,不支持事务 - Wikipedia - MyISAM
答案 1 :(得分:0)
删除以下Hibernate属性,它与Spring事务管理冲突:
<prop key="hibernate.current_session_context_class">thread</prop>