Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用@Transactional时出现“无事务正在进行”错误_Spring_Jpa_Websphere_Spring Transactions - Fatal编程技术网

Spring 使用@Transactional时出现“无事务正在进行”错误

Spring 使用@Transactional时出现“无事务正在进行”错误,spring,jpa,websphere,spring-transactions,Spring,Jpa,Websphere,Spring Transactions,全部,, 我试图在Spring中使用注释性事务并获得 javax.persistence.TransactionRequiredException:没有正在进行的事务 总是出错 Eror stacktrace: [22:40:11:354] 00000024 SystemErr R javax.persistence.TransactionRequiredException: no transaction is in progress [22:40:11:354] 00000024 Sy

全部,, 我试图在Spring中使用注释性事务并获得 javax.persistence.TransactionRequiredException:没有正在进行的事务

总是出错

Eror stacktrace:

[22:40:11:354] 00000024 SystemErr     R javax.persistence.TransactionRequiredException: no transaction is in progress
[22:40:11:354] 00000024 SystemErr     R     at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[22:40:11:354] 00000024 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
[22:40:11:354] 00000024 SystemErr     R     at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
[22:40:11:354] 00000024 SystemErr     R     at $Proxy45.flush(Unknown Source)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[22:40:11:354] 00000024 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[22:40:11:354] 00000024 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
[22:40:11:354] 00000024 SystemErr     R     at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
[22:40:11:354] 00000024 SystemErr     R     at $Proxy42.flush(Unknown Source)
[22:40:11:354] 00000024 SystemErr     R     at com.test.MyDAO.saveOrUpdateData
.
.
.
请注意,当我使用AOP时,同样的代码运行良好

技术堆栈如下所示:

Spring 3.0.5
Struts 2.2.1.1
Hibernate 3.3.2GA
JPA 1.0
JBPM 4.4
以下是我的应用程序配置:

applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <!--    <bean-->
    <!--        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />-->


    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/my_dataSource1.1"
        cache="true" resource-ref="true" lookup-on-startup="true"
        proxy-interface="javax.sql.DataSource" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceXmlLocation" value="META-INF/my-persistence.xml" />
    </bean>

<!--    
Also tried the JTA transaction manager....

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">

-->
    <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>

    <tx:annotation-driven/>


<!--    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--        <tx:attributes>-->
<!--            <tx:method name="get*" read-only="true" />-->
<!--            <tx:method name="saveOrUpdate*" propagation="REQUIRES_NEW" read-only="false"/>-->
<!--        </tx:attributes>-->
<!--    </tx:advice>-->
<!--    <aop:config>-->
<!--        <aop:pointcut id="daoOperations"-->
<!--            expression="execution(* com.honda.naps.dao..*.*(..))" />-->
<!--        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperations" />-->
<!--    </aop:config>-->


     <!-- This is the DAO that's injected -->
     <bean id="myDAO" class="com.test.MyDAO" />
     <bean id="myBaseDAO" class="com.test.BaseDAO" />
     <!-- 
          This is the struts action that's created .. in struts.xml I have used
          <constant name="struts.objectFactory" value="spring" />
      -->    
      <bean id="myAction" scope="prototype" class="com.test.MyAction">
      </bean>

       <!-- My BO instance -->
       <bean id="myBO" class="com.test.MyBOImpl">
          <property name="myDAO" ref="myDAO" />
          <property name="myBaseDAO" ref="myBaseDAO" />
        </bean>

</beans>
BaseDAO类:

使用的图书馆:

系统确认:

我已启用parent_last类加载,并在我的Websphere app server中将默认JPA提供程序分配为org.hibernate.ejb.HibernatePersistence

感谢您的帮助

谢谢

=======================================

将此添加到每个人的使用中

我最终不得不使用基于AOP的事务管理。我找不到在我的应用程序中使用@Transactional的方法

在applicationContext.xml中,将我为AOP集成而添加的部分添加到我的应用程序中:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="saveOrUpdate*" propagation="REQUIRES_NEW" read-only="false"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="daoOperations"
        expression="execution(* com.test..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperations" />
</aop:config>

希望这有帮助,

应用程序上下文应该使用建议的代理来包装myDao。
stacktrace在调用dao.saveOrUpdateData时中断。如果在堆栈跟踪中调查此调用之外的堆栈跟踪,则应该在transactionTemplate.doInTransaction调用中找到一个包装实际DAO方法调用的DAO代理。如果不是这样,那么这个问题可能会显现出来,因为您直接将MyDAOBean注入到BO中。将myDao自动连接到BO可能会起作用。

应用程序上下文应该使用建议的代理来包装myDao。
stacktrace在调用dao.saveOrUpdateData时中断。如果在堆栈跟踪中调查此调用之外的堆栈跟踪,则应该在transactionTemplate.doInTransaction调用中找到一个包装实际DAO方法调用的DAO代理。如果不是这样,那么这个问题可能会显现出来,因为您直接将MyDAOBean注入到BO中。将myDao自动连接到BO可能会起作用。

为什么有两个TransactionManager?如果删除JpaTransactionManager会发生什么情况?能否发布调用saveOrUpdateData的testclass的源代码?有什么理由你的传播等级是新的吗?@abalogh我正在从我的博中召唤道。。顺序是:struts操作->BO->DAO@Ayusman:DAO是如何注入到您的业务对象中的或者更准确地说:BO realy使用Spring管理的刀豆吗?@Ralph和@abalogh,我已经更新了帖子。。。这就是你要找的吗?为什么你有两个交易经理?如果删除JpaTransactionManager会发生什么情况?能否发布调用saveOrUpdateData的testclass的源代码?有什么理由你的传播等级是新的吗?@abalogh我正在从我的博中召唤道。。顺序是:struts操作->BO->DAO@Ayusman:DAO是如何注入到您的业务对象中的或者更准确地说:BO realy使用Spring管理的刀豆吗?@Ralph和@abalogh,我已经更新了帖子。。。这就是你要找的吗?
<class>com.test.MyTbl1</class>
<class>com.test.MyTbl2</class>
<class>com.test.MyTbl3</class>    

<properties>
        <!-- HIBERNATE properties --> 
        <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/> 
        <!--  <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/> -->
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereTransactionManagerLookup"/>
        <property name="jta.UserTransaction" value="java:comp/UserTransaction"/> 
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
        <!--        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>-->
        <property name="hibernate.showsql" value="true"/>
        <!--        <property name="hibernate.cache.use_second_level_cache" value="false"/>        -->
        <!--   <property name="jta.UserTransaction" value="java:comp/UserTransaction"/> -->
        <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect"/> 

</properties>

</persistence-unit>
public class MyDAO extends BaseDAO
{
    @Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
    public void saveOrUpdateData()
    {
        MyTbl1 mt1 = new MyTbl1();
        //do something with mt1
        getEntityManager().persist(mt1);
        getEntityManager().flush();
    }
}
public class BaseDAO {

    /**
     * Variable which stores the injected 
     * JPA Entity Manager.
     */ 
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        System.out.println("getting the entity manager.");
        return entityManager;
    }

    @PersistenceContext(unitName="MY_JPA")
    public void setEntityManager(EntityManager entityManager) {
        logger.debug("Setting the entity manager.");
        this.entityManager = entityManager;
    }

}
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate3.3.2.GA.jar    
antlr-2.7.6.jar
aopalliance-1.0.jar
asm-1.5.3.jar
asm-all-2.2.3.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-digester-1.8.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
dom4j-1.6.1.jar
freemarker-2.3.8.jar
javassist-3.12.0.GA.jar
jbpm.jar
juel-api.jar
juel-engine.jar
juel-impl.jar
log4j-1.2.16.jar
ognl-3.0.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
slf4j-api-1.6.1.jar
slf4j-nop-1.6.1.jar
struts2-convention-plugin-2.2.1.1.jar
struts2-core-2.2.1.1.jar
struts2-dojo-plugin-2.2.1.1.jar
struts2-json-plugin-2.2.1.jar
struts2-spring-plugin-2.2.1.1.jar
struts2-tiles-plugin-2.1.6.jar
tiles-api-2.1.2.jar
tiles-compat-2.1.2.jar
tiles-core-2.1.2.jar
tiles-jsp-2.1.2.jar
tiles-servlet-2.1.2.jar
xwork-core-2.2.1.1.jar
Websphere App server TE 7.0.0.15 in RAD IDE 7.5
OS: Windows XP SP3
DB: MS SQL Server 2008 enterprise edition
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="saveOrUpdate*" propagation="REQUIRES_NEW" read-only="false"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="daoOperations"
        expression="execution(* com.test..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperations" />
</aop:config>