与JNDI和JPA事务管理器进行Spring事务 [英] Spring Transaction with JNDI and JPA transaction manager

查看:77
本文介绍了与JNDI和JPA事务管理器进行Spring事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆