首页 文章

与JNDI和JPA事务管理器的Spring事务

提问于
浏览
1

我在Context.xml(JNDI)中定义了DataSource,我想在Spring应用程序Context.xml中使用JPA事务管理器 . 我不想使用JTA Transaction,因为我使用的是tomcat服务器 .

任何人都可以帮助我如何通过一个例子实现这一目标?我在DAO和服务中使用@transactional注释 .

关心维杰

2 回答

  • 0

    您可以定义DataSource:使用JndiObjectFactoryBean类 . 通过提供JNDI绑定名称来定义数据源 .

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

    这是一个例子:

    <?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章here .

相关问题