I am trying to get Spring Transaction Management working in my new Spring Boot + MyBatis application.
So far I have managed to get everything working with minimal issues - it's just getting the @Transactional
annotation to function correctly. Currently, all statements are committed immediately regardless of whether the method is annotated or not.
Spring Boot does so much of the boilerplate configuration for you that it's difficult to find the missing link.
My build.gradle
contains the following dependencies:
compile("org.springframework.boot:spring-boot-starter-amqp")
compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.0.0")
compile("mysql:mysql-connector-java:5.1.38")
My application.properties
contains the following datasource configuration:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_db
spring.datasource.username=my_user
spring.datasource.password=my_pass
A simple example of a method in a bean that is not acting as expected is as follows:
@Transactional
public void performTransactionTest() throws Exception {
Person person = new Person();
person.setPersonId(123);
personMapper.insert(person);
throw new Exception("This should force a rollback!");
}
The Exception gets thrown but the record has already been inserted.
There is basically no documentation currently in existence on transaction configuration for Spring Boot AND MyBatis together but as far as I understand, it should mostly wire itself up as would be done manually in a Spring + MyBatis application and where it doesn't - we are able to configure it further. With that said I have tried the following configurations in my applicationContext.xml
with no luck:
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
I can confirm that even without any of the above configurations a DataSourceTransactionManager is configured with the same DataSource that the MyBatis mappers' SqlSession uses.
Any help or ideas that could push me in the right direction would be greatly appreciated. If you require any further information I am happy to provide it!
Thanks in advance!
Xandel
sqlSession
to insert or simply using@Insert
mapper annotations? If you are usingsqlSession
please post the relevant code on how you create the object. The same did work for me when i started working on mybatis. I used@Transactional
on method level only. – Will