引发者:javax.persistence.TransactionRequiredException:在带有Spring JTA Transaction的Weblogic上

时间:2018-05-07 06:26:42

标签: java spring hibernate jta atomikos

我正在练习一个带有spring,hibernate库和Weblogic JTA Platform的Web应用程序作为JTA事务管理器,该应用程序已成功部署在Oracle weblogic 12c Server上。当我尝试使用Hibernate DAO执行任何数据库操作时,我收到以下错误

<May 10, 2018 9:40:56 AM IST> <Warning> <org.apache.cxf.phase.PhaseInterceptorChain> <BEA-000000> <Application {http://stockquote.cxf.deployable.wls.com/}StockQuotePortTypeImplService#{http://wls.deployable.cxf.com/stockquote}GetLastTradePrice has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
    at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162)
    at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.createFault(AbstractJAXWSMethodInvoker.java:267)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128)
    at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:232)
    at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:85)
    Truncated. see log file for complete stacktrace
Caused By: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:288)
    at com.sun.proxy.$Proxy263.persist(Unknown Source)
    at com.wls.deployable.cxf.jms.dao.impl.AbstractGenericDAOImpl.save(AbstractGenericDAOImpl.java:54)
    at com.wls.deployable.cxf.jms.bo.impl.MobileBOImpl.save(MobileBOImpl.java:32)
    at com.wls.deployable.cxf.stockquote.StockQuotePortTypeImpl.getLastTradePrice(StockQuotePortTypeImpl.java:51)
    Truncated. see log file for complete stacktrace

请帮帮我。以下是配置文件和DAO类

=====的applicationContext.xml ======================

<!-- Database Configuration -->
    <import resource="../database/DataSource.xml" />
    <import resource="../database/Hibernate.xml" />

    <context:component-scan base-package="com.wls.deployable.cxf" />

    <bean id="stock" class="com.wls.deployable.cxf.stockquote.StockQuotePortTypeImpl" />

    <jaxws:endpoint id="stockService" implementor="#stock" address="/StockService" />

    <bean id="abstractGenericDao" class="com.wls.deployable.cxf.jms.dao.impl.AbstractGenericDAOImpl" abstract="true" />

    <bean id="mobileDAO" class="com.wls.deployable.cxf.jms.dao.impl.MobileDAOImpl" parent="abstractGenericDao" />

    <bean id="mobileBO" class="com.wls.deployable.cxf.jms.bo.impl.MobileBOImpl">
        <property name="mobileDAO" ref="mobileDAO"></property>
    </bean>

=============== DataSource.xml =====================

<jee:jndi-lookup id="devUserDatasource" jndi-name="jdbc.devUserDatasource1" expected-type="javax.sql.DataSource" />

============== Hibernate.xml ==========================

        <import resource="DataSource.xml" />

        <tx:jta-transaction-manager />

        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="oracleEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</prop>
            <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop> 
        </props>
    </property>
    <property name="packagesToScan" value="com.wls.deployable.cxf" />
    <property name="persistenceUnitName" value="oraclePU" />
    <property name="jtaDataSource" ref="devUserDatasource" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/oraclePersistence.xml" />
</bean>

======================== DAO班级===================== =========

public abstract class AbstractGenericDAOImpl<T> implements GenericDAO<T> {

private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericDAOImpl.class);
@PersistenceContext(name="oraclePU")
@Qualifier("oracleEntityManagerFactory")
private EntityManager em;


public EntityManager getEm() {
    return em;
}

public void setEm(EntityManager em) {
    this.em = em;
}


@Transactional(isolation=Isolation.READ_COMMITTED, readOnly=false, rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public T get(Class<T> clazz, Serializable id) {
    return em.find(clazz, id);
}
@Transactional(isolation=Isolation.READ_COMMITTED, readOnly=false, rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public void update(T domain) {
    em.merge(domain);   
}

@Transactional(isolation=Isolation.READ_COMMITTED, readOnly=false, rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public void remove(T domain) {
    em.remove(domain);  
}


@Transactional(isolation=Isolation.READ_COMMITTED, readOnly=false, rollbackFor=Exception.class, propagation=Propagation.REQUIRED)
public void save(T domain) {
    if(em != null) {
        LOG.info("========> PERSISTING MOBILE <========");
    }
    em.persist(domain);


}

}

======================== WebService Impl ===================== ==

@WebService(endpointInterface="com.cxf.deployable.wls.stockquote.StockQuotePortType")
public class StockQuotePortTypeImpl implements StockQuotePortType {


@Autowired
MobileBOImpl mobileBO;

@Autowired
DataSource ds;

private static final Logger LOGGER = LoggerFactory.getLogger(StockQuotePortTypeImpl.class);

@Override
public TradePrice getLastTradePrice(TradePriceRequest body) {

    JdbcTemplate temp = new JdbcTemplate(ds);
    temp.update("UPDATE MOBILE SET MODELNAME ='iPhone' WHERE ID = 3");
    LOGGER.info("Done JDBCTEMPLATE update ...");

    Mobile mobile = new Mobile();

    mobile.setId(9);
    mobile.setBrand("Samsung");
    mobile.setModelName("Galaxy S8");
    mobile.setOs("Android");

    LOGGER.info("Update DB with Hibernate BO/DO .. !!!");
    mobileBO.save(mobile);
    LOGGER.info("Database updated .. !!!");


    TradePrice tradePrice = new TradePrice();
    tradePrice.setPrice(100f);

    return tradePrice;
}

}

================== oraclePersistence.xml ===================

<persistence-unit name="oraclePU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>devUserDatasource</jta-data-source>
        <class>com.distributed.tx.demo.distTxDemo.entities.Feature</class>
        <class>com.distributed.tx.demo.distTxDemo.entities.Mobile</class>
    </persistence-unit>

==================的web.xml ========================== ==

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/config/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

下面是我的maven依赖树

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ wls-deployable-cxf ---
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO] wls.springcxf:wls-deployable-cxf:war:0.0.1-SNAPSHOT
[INFO] +- com.oracle:ojdbc6:jar:11.2.0:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.12:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-all:jar:1.3:test
[INFO] +- org.apache.cxf:cxf-rt-frontend-jaxws:jar:3.2.1:compile
[INFO] |  +- xml-resolver:xml-resolver:jar:1.2:compile
[INFO] |  +- org.apache.cxf:cxf-core:jar:3.2.1:compile
[INFO] |  |  +- com.fasterxml.woodstox:woodstox-core:jar:5.0.3:compile
[INFO] |  |  |  \- org.codehaus.woodstox:stax2-api:jar:3.1.4:compile
[INFO] |  |  \- org.apache.ws.xmlschema:xmlschema-core:jar:2.2.2:compile
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-soap:jar:3.2.1:compile
[INFO] |  |  +- org.apache.cxf:cxf-rt-wsdl:jar:3.2.1:compile
[INFO] |  |  |  \- wsdl4j:wsdl4j:jar:1.6.3:compile
[INFO] |  |  \- org.apache.cxf:cxf-rt-databinding-jaxb:jar:3.2.1:compile
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-xml:jar:3.2.1:compile
[INFO] |  +- org.apache.cxf:cxf-rt-frontend-simple:jar:3.2.1:compile
[INFO] |  \- org.apache.cxf:cxf-rt-ws-addr:jar:3.2.1:compile
[INFO] |     \- org.apache.cxf:cxf-rt-ws-policy:jar:3.2.1:compile
[INFO] |        \- org.apache.neethi:neethi:jar:3.1.0:compile
[INFO] +- org.apache.cxf:cxf-rt-transports-http:jar:3.2.1:compile
[INFO] +- org.apache.cxf.xjc-utils:cxf-xjc-runtime:jar:3.0.5:compile
[INFO] |  \- javax.xml.bind:jaxb-api:jar:2.2.12:compile
[INFO] +- org.springframework:spring-web:jar:5.0.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-beans:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:5.0.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-expression:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:5.0.5.RELEASE:test
[INFO] +- org.springframework:spring-jms:jar:5.0.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-messaging:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-asm:jar:3.1.4.RELEASE:compile
[INFO] +- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.8.13:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] +- org.hibernate:hibernate-core:jar:4.3.1.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.1.Final:compile
[INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] +- antlr:antlr:jar:2.7.7:compile
[INFO] +- javassist:javassist:jar:3.12.0.GA:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] +- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- javax.servlet:servlet-api:jar:3.0-alpha-1:compile
[INFO] +- org.apache.activemq:activemq-all:jar:5.15.3:compile
[INFO] +- com.oracle.weblogic:weblogic-t3thinclient:jar:1.0:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.7:compile
[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.ow2.asm:asm:jar:6.1.1:compile
[INFO] +- org.springframework:spring-aspects:jar:5.0.5.RELEASE:compile
[INFO] \- cglib:cglib-nodep:jar:3.2.6:compile

请帮帮我这里很长一段时间我都在挣扎。

提前致谢。!!

1 个答案:

答案 0 :(得分:0)

我找到了我的问题的解决方案,我在我的spring的applicationContext.xml中缺少tx:annotationDriven元素。添加元素启动了Spring aop启用的事务管理,它使用WeblogicJtaTransactionManager来管理事务隐含.. !!