【问题标题】:PersistenceUnitInfo [appName] has transactionType JTA, but does not have a jtaDataSource definedPersistenceUnitInfo [appName] 具有 transactionType JTA,但没有定义 jtaDataSource
【发布时间】:2014-05-20 15:48:14
【问题描述】:

我是 WebLogic 新手,对 Hibernate / Spring 应用程序还很陌生,因为我的主要语言是 C#,而我的主要服务器一直是 Windows 服务器,所以请原谅我可能遇到的任何简单错误。

我无法部署到我们的 WebLogic 10.3.4 服务器。它在我的 WebLogic 实例上本地工作,但不在远程服务器上。

我将 Hibernate 4.2.8 用于持久性,并将 Spring MVC 4.0 用于我的 Web 应用程序框架。我收到的错误是:

Failed to load webapp: 'ncms2_May20.war'

Message icon - Error Substituted for missing class Exception [EclipseLink-28010] (Eclipse Persistence Services - 2.1.2.v20101206-r8635) - org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: PersistenceUnitInfo ncms2 has transactionType JTA, but does not have a jtaDataSource defined.

我正在使用基于 Spring 注释的 Hibernate 配置文件。

package mil.navy.navsupbsc.utilities;

import java.util.Properties;

import javax.sql.DataSource;

import com.app.AuditInterceptor;

import org.hibernate.SessionFactory;
import org.hibernate.dialect.Oracle10gDialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;

@Configuration
public class HibernateConfiguration {

    @Value("#{dataSource}")
    private DataSource dataSource;


    @Bean
    public LocalSessionFactoryBean sessionFactoryBean() {
        Properties props = new Properties();
        props.put("hibernate.dialect", Oracle10gDialect.class.getName());
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.hbm2ddl.auto", "update");
        props.put("hibernate.show_sql", "true");
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.use_sql_comments", "true");
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setEntityInterceptor(new AuditInterceptor());
        bean.setPackagesToScan(new String[] { "com.app.entity" });           
                bean.setHibernateProperties(props);     
        bean.setDataSource(this.dataSource);

        return bean;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(sessionFactoryBean().getObject());
    }

}

我的 Spring Servlet XML:

    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="com.app" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

你们中的任何人都可以帮助我完成这项工作吗?我感谢任何和所有的帮助。谢谢!

更新

我做了以下切换到 JTA:

  • 在 Hibernate java 配置文件中添加了 JtaTransactionManager,而不是使用 HibernateTransactionManager。
  • 在 LocalSessionFactoryBean 中设置 JtaTransactionManager 属性
  • 在 spring-servlet 文件中添加了 jdni 查找
  • 在 web.xml 和 weblogic.xml 文件中添加了资源引用
  • 删除了 spring-servlet 中的数据源,并删除了 hibernate java 配置文件中对它的引用
  • 在服务器上创建了一个数据源

但仍然不能 100% 工作。将保持更新。

更新:这是我一直在使用的有用资源 (http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/)

【问题讨论】:

  • 如果您使用的是 Hibernate,为什么您的错误消息是指 eclipse 链接?
  • 我不知道 - 虽然我知道 EclipseLink 已安装在服务器上,但我并没有在应用程序中的任何内容中提及 EclipseLink。
  • 这就是为什么我真的很困惑。由于我没有使用它,我不知道在应用程序中要更改什么......可能是服务器配置问题?

标签: java spring hibernate jpa transactions


【解决方案1】:

您尚未发布您的 persistence.xml JPA 配置文件,但它可能包含以下内容:

<persistence-unit name="..." transaction-type="JTA">
<jta-data-source>java:/DefaultDS</jta-data-source>

在应用服务器上部署时,您应该受益于它们自己的 JTA 数据源和事务管理器支持,因此通过 JNDI 定位 jtaDataSource 应该是您的首选。

你的数据源是用 Spring 配置的:

org.springframework.jdbc.datasource.DriverManagerDataSource

这并不是真正的生产就绪数据源实现。

因此,尝试配置您的 Spring 应用程序上下文以利用 WebLogic 事务管理支持。

【讨论】:

  • 好的 - 我想我理解了这个概念 - 我将努力实现它并希望让它发挥作用 - 我假设它类似于创建 WebLogicJtaTransactionManager?关于如何做到这一点的好资源有什么建议吗?我现在正在查看一些 Spring 文档 (docs.spring.io/spring/docs/2.0.x/reference/transaction.html) 但它是针对旧版本的 Spring
  • 这个web logic article 可能会对你有所帮助。
  • 我快速阅读它,我会再次阅读它以提取相关信息,但它指的是较低版本的 Spring,并且没有使用注释或使用包扫描的自动装配。应该有助于理解概念,所以谢谢!
  • Spring 4.0.x 仍然包含WebLogicJtaTransactionManager。 TransactionProxyFactoryBean 被 @Transactional 注释替换,它应该应用于您的 Service 方法。还有这个newer doc section
  • Persistence.xml 是 JPA 要求。春天不能代替它。您的问题名称是 JPA 异常,因此您从某个地方继承了 persistence.xml。检查 jars/META-INF 文件夹内部。
【解决方案2】:

我使用@Vlad Mihalcea 在答案中提供的信息来改进我的代码;但是,这不是它不起作用的原因。 @Vlad 的一个 cmets 建议在 META-INF 文件夹中查找 persistence.xml 文件,这让我得到了答案。

即使我使用的是 Spring 配置文件用于 Hibernate,但我在 META-INF 文件夹下有一个旧的 persistence.xml 文件。我没有使用它,但 WebLogic 会自动拾取它。由于我没有使用它,因此我没有在 persistence.xml 文件中指定数据源。如果未指定为 RESOURCE LOCAL,WebLogic 会自动假定任何持久性单元都是 JTA。我删除了该 persistence.xml 文件,它起作用了。

也就是说,我还按照@Vlad Mihalcea 的建议修复了我的代码以使用 JTA 数据源。我将所有配置代码移至 Spring-servlet.xml 文件以简化配置。我确信它可以毫不费力地转换成程序化的 Spring 配置。

同时,这里有一个基于 JTA 事务的 Spring / Hibernate 配置文件。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="mil.navy.navsupbsc.entity" />
        <property name="dataSource" ref="NCS"/>
        <property name="jtaTransactionManager" ref="transactionManager" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform
                </prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.driverClassName">oracle.jdbc.OracleDriver</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
            </props>
        </property>
    </bean>

    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <context:component-scan base-package="mil.navy.navsupbsc" />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <jee:jndi-lookup id="NCS" jndi-name="NCS" resource-ref="false">
    </jee:jndi-lookup>
    <tx:jta-transaction-manager id="transactionManager" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

请注意,我还需要将 web.xml 和 WebLogic.xml 文件中的引用添加到我在 Web 服务器上创建的 JNDI 数据源(在我的应用程序中称为 NCS)。

【讨论】:

  • 干得好,劳拉!感谢您发布最终配置。其他陷入困境的人肯定会发现它很有用。
  • 我很高兴终于弄明白了!这很令人沮丧,但在我的掌握中获得更多知识是件好事。希望它会为其他人省去一些麻烦。非常感谢您的所有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多