【问题标题】:Spring Transaction with JNDI and JPA transaction manager带有 JNDI 和 JPA 事务管理器的 Spring Transaction
【发布时间】:2012-10-25 13:44:11
【问题描述】:

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

谁能帮助我如何通过示例实现这一目标?我在 DAO 和服务中使用 @transactional 注释。

问候 维杰

【问题讨论】:

    标签: spring jpa-2.0


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这是一个例子:

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

      使用&lt;jee:jndi-lookup/&gt; 访问JNDI 数据源。然后,将获得的数据源引用传递给适当的PlatformTransactionManager,例如DataSourceTransactionManager

      此外,应提供&lt;tx:annotation-driven /&gt; 以激活@Transactional 注释。

      为了更好地理解 Spring 事务管理,我建议阅读 Spring 参考的第 10 章here

      【讨论】:

      • 别忘了tx:annotationDriven
      • @Reza 但我面临以下问题 org.springframework.beans.factory.BeanCreationException:创建名称为“purpleDS”的 bean 时出错:调用 init 方法失败;嵌套异常是 javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
      • @VijayM 对不起,我迟到了;回答完之后我就上床睡觉了 :-) 我确定原因是根本没有定义 JDNI 数据源,或者在 Spring 配置中使用了错误的名称。请仔细检查这部分并使用正确的名称:jndi-name=""
      • @Reza 感谢您的回复!我解决了上述问题,但现在我面临以下问题 javax.persistence.PersistenceException: [PersistenceUnit: CoreProject] Unable to build EntityManagerFactory,Caused by: org.hibernate.HibernateException: unknown Oracle major version [11] 我已经明确定义了上下文.xml、peristence.xml(休眠持久化提供程序)、spring.xml(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean、org.springframework.orm.jpa.JpaTransactionManager)和Server.xml。我不知道我现在错过了什么。
      • 这很可能是因为 Hibernate 使用了错误的方言。这必须帮助你:maniezhilan.blogspot.com.au/2009/02/…
      猜你喜欢
      • 1970-01-01
      • 2019-02-24
      • 2017-12-23
      • 2012-09-26
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多