问题描述
我已经在Context.xml(JNDI)中定义了数据源,并且我想与Spring应用程序Context.xml中的JPA事务管理器一起使用.我不想使用JTA事务,因为我正在使用tomcat服务器.
I have defined DataSource in Context.xml(JNDI) and i would like to use with JPA transaction manager in Spring application Context.xml. I don't want to use JTA Transaction since i am using tomcat server.
有人可以帮我一个例子吗?我在DAO和服务中使用@transactional批注.
Could anyone help me how can i achieve this with an example? I am using @transactional annotations in DAO's and services.
问候维杰
推荐答案
以下是示例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Provides access to the JNDI datasource -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/>
<!-- Defines transaction manager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Enables transactional behavior -->
<tx:annotation-driven />
<!-- other <bean/> definitions here -->
</beans>
使用<jee:jndi-lookup/>
可以访问JNDI
数据源.然后,将获得的数据源引用传递到适当的PlatformTransactionManager
,例如DataSourceTransactionManager
.
Use <jee:jndi-lookup/>
to get access to the JNDI
data source. Then, pass the obtained datasource reference to an appropriate PlatformTransactionManager
such as DataSourceTransactionManager
.
此外,应提供<tx:annotation-driven />
以便激活@Transactional
批注.
Also, <tx:annotation-driven />
should be provided for the @Transactional
annotations to be activated.
为了更好地理解Spring事务管理,我建议阅读此处.
To obtain a good understanding of Spring transaction management, I would recommend reading chapter 10 of Spring reference available here.
这篇关于与JNDI和JPA事务管理器进行Spring事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!