Spring注解事务管理 [英] Spring annotation transaction mangement
问题描述
我有一个 Spring &我将在其中连接到两个数据库的 Hibernate Web 应用程序.
I have a Spring & Hibernate web application in which I would to connect to two databases.
在 web.xml
中,我将应用程序上下文文件的路径配置如下
In the web.xml
I have configured the path of the application context files as following
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/*.xml
</param-value>
</context-param>
我在config目录下有三个文件
where I have three files under the config directory
- 1-primary-hibernateContext.xml
- 2-secondary-hibernateContext.xml
- 3- 根上下文.xml
在第一个文件中,我配置了主数据源、会话工厂和事务管理器(primary_manager)
In the first file I have configured the primary data source, session factory, and transaction manager(primary_manager)
我在第二个文件中做了同样的事情
I did the same in the second file
在第三个文件中,我配置了组件扫描,事务注解配置如下
In the third file I have configured the component scan, and transaction annotation configuration as following
<tx:annotation-driven/>
<!-- Enable Spring annotation configuration -->
<context:annotation-config />
<!-- Scan the application for all possible Services & autowire -->
<context:component-scan base-package="net.mr2.*.service" />
<context:component-scan base-package="net.mr2.*.dao" />
我有一个 servlet 上下文 xml 文件,在其中我已将调度程序 servlet 配置为如下
I have a servlet context xml file in which I have configured the dispatcher servlet as following
<annotation-driven />
<context:component-scan base-package="net.mr2.*.web" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
服务类被注解@service
&@Transactional(value="secondary_transactionManager", readOnly=true, rollbackFor=Exception.class)
.
The service class is annotated @service
& @Transactional(value="secondary_transactionManager", readOnly=true, rollbackFor=Exception.class)
.
我有一个控制器,其中的服务是自动装配的.
I have a single controller in which the service is autowired.
我从控制器调用服务类中的三个方法(save1()
、save2()
、save3()
).我在第三种方法中抛出异常,我认为第三种方法应该回滚,但这不会发生.
From the controller I call three methods in the service class (save1()
, save2()
, save3()
). and I throw exception in the third method and I assume that the third method should rollback but this does not happen.
我打开了mysql日志,查看了三个保存语句,发现语句不是在一个事务中执行的.我意识到,因为我没有在插入语句之前和之后找到 SET AUTOCOMMIT
语句.
I have opened the mysql logs and checked the three saving statements and I found that the statements are not executed in a transaction. I realized that as I did not find SET AUTOCOMMIT
statements before and after the insert statement.
所以,我假设交易没有配置好.
So, I assume that the transactions are not configured well.
我的设置会出现什么问题?
What would be the problem in my settings ?
推荐答案
我意识到我用错误的事务管理器注释了服务,这导致了以前的奇怪行为
I have realized that I annotated the service with the wrong transaction manager and this what leads to the previous strange behavior
服务(我正在使用)用
The service (I was using) was annotated with
@transactional(value='transaction_manger_1')
@transactional(value='transaction_manger_1')
这个事务管理器被配置为使用只读数据库,而注入的 DAO 使用正确的会话工厂,它与读/写数据库一起工作.
This transaction manager is configured to work with the read_only DB while the injected DAO was using the correct session factory which works with the read/write DB.
所以我已经将事务管理器更改为正确的,并且一切正常.
So I have changed the transaction manager to the correct one and every thing goes fine.
这篇关于Spring注解事务管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!