gpt4 book ai didi

mysql - Spring Hibernate MySQL 测试不回滚

转载 作者:行者123 更新时间:2023-11-29 04:57:45 26 4
gpt4 key购买 nike

我在使用 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

是的,我在谷歌上搜索了很多这个问题,但没有真正找到解决方案。我不知道出了什么问题。

有什么建议吗?

最佳答案

您使用的是 InnoDB 引擎吗? MyISAM 是 MySQL 5.x 5.5 之前的默认引擎,不支持事务 - Wikipedia - MyISAM

关于mysql - Spring Hibernate MySQL 测试不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4948811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com