Spring Transaction with JNDI and JPA transaction manager

天大地大妈咪最大 提交于 2021-01-28 21:11:01

问题


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.

Could anyone help me how can i achieve this with an example? I am using @transactional annotations in DAO's and services.

Regards Vijay


回答1:


you can Define DataSource: Use JndiObjectFactoryBean class. To define the data source by providing JNDI binding name.

<!– Data Source JNDI –>
<bean id=”dataSource” class=”org.springframework.jndi.JndiObjectFactoryBean”>
<property name=”jndiName”>
<value>jdbc/SampleDS</value>
</property>
</bean>



回答2:


Here is an example:

<?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>

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.

Also, <tx:annotation-driven /> should be provided for the @Transactional annotations to be activated.

To obtain a good understanding of Spring transaction management, I would recommend reading chapter 10 of Spring reference available here.



来源:https://stackoverflow.com/questions/13069999/spring-transaction-with-jndi-and-jpa-transaction-manager

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!