【问题标题】:Migration weblogic to tomcat : No JTA UserTransaction available将 weblogic 迁移到 tomcat:没有可用的 JTA UserTransaction
【发布时间】:2019-02-03 23:17:41
【问题描述】:

我在一个多模块的 maven 应用程序上工作,并且 weblogic 已经在生产中。堆栈是:

Tomcat 8.5

PostGre 9.6

休眠 4.3.8

春季 4.1.5

Spring 安全 3.2.6

弹簧支柱 3.2.13

素数面孔 5.1

我开始迁移,在 server.xml 中创建 JNDI 数据源并在 META-INF/context.xml 中创建资源链接(我们将 DB 从 DB2 移动到 PostGre),但我遇到了一个异常:

原因:java.lang.IllegalStateException:没有可用的 JTA UserTransaction - 指定“userTransaction”或“userTransactionName”或“transactionManager”或“transactionManagerName”

我的应用程序在 xml 配置上,我认为错误发生是因为 JTA 实现在旧版本中被委托给 weblogic:

DataSourceContext.xml

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/mydatasourcename" />
</bean>

<bean id="hibernateProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.max_fetch_depth">1</prop>
            <prop key="hibernate.default_schema">defaultschemaname</prop>
            <!-- How to find the Transaction -->
            <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
            </prop>

            <!-- How to produce transaction -->

            **<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform
            </prop>**

            <!-- Session context with JTA -->
            <prop key="current_session_context_class">jta</prop>
        </props>
    </property>
</bean>

我的 Tomcat 数据源:

<GlobalNamingResources>
<!-- Editable user database that can also be used by
     UserDatabaseRealm to authenticate users
-->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" readonly="true" type="org.apache.catalina.UserDatabase"/>
    <Resource auth="Container" driverClassName="org.postgresql.Driver" 
         maxIdle="10"
         maxTotal="20"
         maxWaitMillis="10000" 
         name="jdbc/*****" 
         password="*****" 
         type="javax.sql.DataSource" 
         url="jdbc:postgresql://********:5432/****" 
         username="****"/>  
</GlobalNamingResources>

休眠配置 applicationContextDAO.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan">
            <list>
              <value>package.name</value>
            </list>
        </property> 
        <property name="annotatedPackages">
            <list>
                <value>package.name</value>
            </list>
        </property>
        <property name="hibernateProperties" ref="hibernateProperties" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="saveHibernateListener" class="package.name.hibernateListener.SaveHibernateListener"/>

    <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <bean class="package.name.CustomDateEditorRegistrar"/>
            </list>
        </property>
    </bean>

<!-- Template hibernate injecté dans chaque persisteur -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
</beans>

门面模块 applicationContext-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="get*" propagation="REQUIRED" read-only="true"
            rollback-for="ExceptionWork" />
        <tx:method name="*" propagation="REQUIRED" rollback-for="MessageException" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="ivaFacadePointcut"
        expression="execution(* package.name..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="FacadePointcut" />
</aop:config>

<aop:aspectj-autoproxy proxy-target-class="true"/>

<tx:jta-transaction-manager />
<tx:annotation-driven/>


我想我必须在 DataSourceContext.xml 中替换这个道具: org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform ?

在 tomcat 8.5 中实现事务管理器的现有干净解决方案?还是独立的事务管理器?

我对我已经找到的所有解决方案感到困惑(旧版本的休眠或 JTA 的折旧实现)...如果有人能启发我,我将不胜感激 :)

【问题讨论】:

    标签: java spring hibernate tomcat jta


    【解决方案1】:

    虽然 Weblogic 是一个完整的应用服务器,但 Tomcat 在手中只是一个 Web 容器。因此,您需要缩小这两者之间的一些差距。

    事务管理器就是其中之一。 Weblogic 会“自动”为您添加一个,而 Tomcat 没有这样做的能力。

    如果你使用下面的 sn-p 之类的东西,你应该能够解决这个问题。

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    

    【讨论】:

    • 感谢您的支持,我仍然有相同的执行。实际配置似乎在等待 JTA 事务管理器。我将使用事务 xml 配置编辑我的帖子。
    • 在我的回答中尝试使用 org.springframework.transaction.jta.JtaTransactionManager 而不是 Jpa
    • 这是个好主意,我发现他们在项目的测试端使用了这个:
    • 您遇到的问题是您的 tx 管理器没有为 Tomcat 正确设置...不幸的是,我现在有点忙于工作,无法完全为您提供完整的工作例子。您的 HibernateTransactionManager 应该可以与 Tomcat 一起正常工作,因此您可以将 Jta 替换为休眠的
    • 再次感谢,我会这样探索。
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 2011-04-18
    • 2015-05-10
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2014-10-01
    相关资源
    最近更新 更多