簡體   English   中英

與JNDI和JPA事務管理器進行Spring事務

[英]Spring Transaction with JNDI and JPA transaction manager

我已經在Context.xml(JNDI)中定義了數據源,並且我想與Spring應用程序Context.xml中的JPA事務管理器一起使用。 我不想使用JTA事務,因為我正在使用tomcat服務器。

有人可以幫我一個例子嗎? 我在DAO和服務中使用@transactional批注。

問候維傑

這是一個例子:

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

另外,應提供<tx:annotation-driven />來激活@Transactional注釋。

為了更好地理解Spring事務管理,我建議閱讀這里的Spring參考書的第10章。

您可以定義數據源:使用JndiObjectFactoryBean類。 通過提供JNDI綁定名稱來定義數據源。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM