开发者

How to use transactions with Spring/MyBatis? Best practice?

开发者 https://www.devze.com 2023-03-30 10:33 出处:网络
I am trying to use transactions with MyBatis and Spring and was wondering if there is a best practice on how to achieve this? Any hints or thoughts are appreciated.

I am trying to use transactions with MyBatis and Spring and was wondering if there is a best practice on how to achieve this? Any hints or thoughts are appreciated.

My application wil be running in a tomcat containe开发者_运维知识库r against a MySQL database.


You want to take a look at the @Transactional annotation docs In terms of best practices it is a mixture of database transactions and spring. Look at where you need to roll back your data, do you need JTA, etc.

Example class

@Transactional
public class DefaultFooService implements FooService {

   Foo getFoo(String fooName);
}

Example xml

<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- enable the configuration of transactional behavior based on annotations -->         <tx:annotation-driven transaction-manager="txManager"/>

<!-- a PlatformTransactionManager is still required -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
    <property name="dataSource" ref="dataSource"/>
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号