春天注解交易管理CHINESE [英] Spring annotation transaction mangement
问题描述
我有一个弹簧和放大器; 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-初级hibernateContext.xml
- 2 - 次级hibernateContext.xml
- 3根context.xml中
在第一个文件我已经配置了主数据源,会话工厂和事务管理器(primary_manager)
In the first file I have configured the primary data source, session factory, and transaction manager(primary_manager)
我做第二个文件相同
在第三个文件我已经配置了部分扫描,交易注解配置如下
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作为继servlet上下文xml文件
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(值=secondary_transactionManager,只读=真,的rollbackFor = Exception.class)
。
我具有其中该服务被自动装配一个控制器。
I have a single controller in which the service is autowired.
从控制器I调用服务类三种方法(保存1()
, 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 ?
推荐答案
我已经意识到我标注了错误的事务管理器的服务,这是什么导致了previous奇怪的行为
I have realized that I annotated the service with the wrong transaction manager and this what leads to the previous strange behavior
服务(我使用)与注释
@Transactional(值='transaction_manger_1')
@transactional(value='transaction_manger_1')
此事务管理器被配置,而注入的DAO用的是正确的会话工厂它与读/写块的工作原理与READ_ONLY DB来工作。
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.
这篇关于春天注解交易管理CHINESE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!