【发布时间】: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