我正在尝试JOOQ,我首先想到的是运行一个会自动回滚的事务单元测试。我似乎没有让它工作,我不知道从这里去哪里。日志显示事务已启动并回滚。但是更改仍然会提交到数据库。
如何使Spring中的事务能够正常使用JOOQ?
2013-11-04 23:41:14,353 [main] DEBUG DataSourceTransactionManager: Creating new transaction with name [insertPersons]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2013-11-04 23:41:14,369 [main] DEBUG DataSourceTransactionManager: Acquired Connection [jdbc:mysql://localhost/jooq-learn, UserName=jooq@localhost, MySQL-AB JDBC Driver] for JDBC transaction
2013-11-04 23:41:14,376 [main] DEBUG DataSourceTransactionManager: Switching JDBC Connection [jdbc:mysql://localhost/jooq-learn, UserName=jooq@localhost, MySQL-AB JDBC Driver] to manual commit
2013-11-04 23:41:14,507 [main] DEBUG DataSourceTransactionManager: Initiating transaction rollback
2013-11-04 23:41:14,508 [main] DEBUG DataSourceTransactionManager: Rolling back JDBC transaction on Connection [jdbc:mysql://localhost/jooq-learn, UserName=jooq@localhost, MySQL-AB JDBC Driver]
2013-11-04 23:41:14,509 [main] DEBUG DataSourceTransactionManager: Releasing JDBC Connection [jdbc:mysql://localhost/jooq-learn, UserName=jooq@localhost, MySQL-AB JDBC Driver] after transaction
2013-11-04 23:41:14,509 [main] DEBUG DataSourceUtils: Returning JDBC Connection to DataSource
我的测试看起来像:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(TestConstants.contextConfigFile)
public class JooqTest {
private static final Logger logger = LoggerFactory.getLogger(JooqTest.class);
private PersonService personService;
@Inject
public void setPersonService(PersonService personService) {
this.personService = personService;
}
@Test
@Transactional
public void insertPersons() {
PersonRecord john = new PersonRecord();
john.setName("John");
john.setAge(45);
personService.save(john);
}
}
我从JOOQ网站上获取了Spring上下文配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
init-method="createDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/jooq-learn"/>
<property name="username" value="jooq"/>
<property name="password" value="*****"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionAwareDataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg ref="dataSource"/>
</bean>
<bean class="org.jooq.impl.DataSourceConnectionProvider" name="connectionProvider">
<constructor-arg ref="transactionAwareDataSource"/>
</bean>
<bean id="dsl" class="org.jooq.impl.DefaultDSLContext">
<constructor-arg ref="config"/>
</bean>
<bean class="org.jooq.impl.DefaultConfiguration" name="config">
<constructor-arg index="0" ref="connectionProvider"/>
<constructor-arg index="1">
<null/>
</constructor-arg>
<constructor-arg index="2">
<null/>
</constructor-arg>
<constructor-arg index="3">
<null/>
</constructor-arg>
<constructor-arg index="4">
<null/>
</constructor-arg>
<constructor-arg index="5">
<value type="org.jooq.SQLDialect">MYSQL</value>
</constructor-arg>
<constructor-arg index="6">
<null/>
</constructor-arg>
<constructor-arg index="7">
<null/>
</constructor-arg>
</bean>
答案 0 :(得分:6)
MySQL(至少旧版本)默认使用MyISAM作为表类型,此表类型不支持事务(有关支持选项,请参阅表14.7)。您需要切换到支持事务的InnoDB表。
来源:MySQL documentation
链接