当前位置: 首页 > 知识库问答 >
问题:

Spring 4.0.6 Weblogic 10.3.6 EJB3.0 CMT Spring数据JPA 1.6.2的事务异常——“绑定到外部管理事务的错误”

严阳秋
2023-03-14

我在使用Weblogic 10.3.6、Spring 4.0.6、Spring Data JPA 1.6.2、Eclipselink(复制了Weblogic embedded 2.3版本和最新的2.5.2)和容器管理事务时遇到了一个奇怪且难以调试的问题。(我更新了Weblogic,使用JPA2.0安装Oracle提供的补丁)。

问题如下:我有一个简单的、无状态的EJB3.0,其中使用了Spring Autowiring和Spring数据jpa存储库。

当我调用EJB公开的任何服务时,我会得到以下奇怪的异常(请注意,我必须找到/替换所有真实的类名/路径):

EJB Exception: : com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke public void org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(javax.interceptor.InvocationContext) on bean class class 
    ... 58 more
Caused by: org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath*:beanRefContext.xml], factory key [null]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainApplicationContext' defined in URL [file:/C:/work/project/spring-ejb-project/target/classes/beanRefContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actionTBConverter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.dao.repositories.personRepository myproject.converters.DataConverter.personRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:387) 
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactoryReference(SpringBeanAutowiringInterceptor.java:160)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactory(SpringBeanAutowiringInterceptor.java:141)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.doAutowireBean(SpringBeanAutowiringInterceptor.java:121)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(SpringBeanAutowiringInterceptor.java:95)
    ... 63 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainApplicationContext' defined in URL [file:/C:/work/project/spring-ejb-project/target/classes/beanRefContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actionTBConverter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.dao.repositories.personRepository myproject.converters.DataConverter.personRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.access.ContextSingletonBeanFactoryLocator.initializeDefinition(ContextSingletonBeanFactoryLocator.java:143)
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:382)
    ... 67 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actionTBConverter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.dao.repositories.personRepository myproject.converters.DataConverter.personRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270)
    ... 80 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'actionTBConverter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.dao.repositories.personRepository myproject.converters.DataConverter.personRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 82 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.dao.repositories.personRepository myproject.converters.DataConverter.personRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 99 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 101 more
Caused by: javax.persistence.PersistenceException:
Exception Description: Error binding to externally managed transaction
Internal Exception: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.registerIfRequired(JTATransactionWrapper.java:145)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.setJTATransactionWrapper(EntityManagerImpl.java:2153)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.detectTransactionWrapper(EntityManagerImpl.java:878)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.initialize(EntityManagerImpl.java:401)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.<init>(EntityManagerImpl.java:394)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:321)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    at sun.reflect.GeneratedMethodAccessor257.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at weblogic.deployment.EntityManagerFactoryProxyImpl.invoke(EntityManagerFactoryProxyImpl.java:96)
    at com.sun.proxy.$Proxy59.createEntityManager(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:283)
    at com.sun.proxy.$Proxy160.createNamedQuery(Unknown Source)
    at org.springframework.data.jpa.repository.query.NamedQuery.<init>(NamedQuery.java:62)
    at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:113)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:132)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:166)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 111 more
Caused by: weblogic.transaction.RollbackException: setRollbackOnly called on transaction
    at weblogic.transaction.internal.TransactionImpl.throwRollbackException(TransactionImpl.java:1884)
    at weblogic.transaction.internal.ServerTransactionImpl.registerSynchronization(ServerTransactionImpl.java:639)
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.registerIfRequired(JTATransactionWrapper.java:136)
    ... 137 more
Caused by: weblogic.transaction.internal.AppSetRollbackOnlyException: setRollbackOnly called on transaction
    at weblogic.transaction.internal.TransactionImpl.setRollbackOnly(TransactionImpl.java:555)
    at weblogic.transaction.internal.TransactionManagerImpl.setRollbackOnly(TransactionManagerImpl.java:340)
    at weblogic.transaction.internal.TransactionManagerImpl.setRollbackOnly(TransactionManagerImpl.java:333)
    at org.eclipse.persistence.transaction.JTATransactionController.markTransactionForRollback_impl(JTATransactionController.java:160)
    at org.eclipse.persistence.transaction.AbstractTransactionController.markTransactionForRollback(AbstractTransactionController.java:216)
    at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.setRollbackOnlyInternal(JTATransactionWrapper.java:85)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.setRollbackOnly(EntityManagerImpl.java:2092)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1127)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:289)
    ... 124 more
; nested exception is: com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke public void org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(javax.interceptor.InvocationContext) on bean class class org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor_s85z0p_Impl with args: [com.oracle.pitchfork.intercept.InterceptionMetadata$LifecycleEventCallbackInvocationContext@cb3ef49]

我真的很难理解为什么会发生这种情况,我希望有人能抽出时间来帮助我。

(我删除了xml命名空间来保存字符,并删除了真正的项目类名)

这是我的beanRefContext。xml

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

    <bean id="mainApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
                <value>classpath*:/spring-config.xml</value>
            </list>
        </constructor-arg>
    </bean>


</beans>

这是我的spring配置。xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...
        default-lazy-init="false">

    <context:annotation-config/>
    <context:component-scan base-package="myproject"/>

</beans>

以下是我的Spring配置java文件:

@Configuration
@EnableLoadTimeWeaving
@EnableJpaRepositories("myproject.dao.repositories")
public class SpringConfig implements ApplicationContextAware, LoadTimeWeavingConfigurer {

    private static final Logger LOG = LoggerFactory.getLogger(SpringConfig.class);

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext ac) {
        this.applicationContext = ac;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() throws Exception {
        Context ctx = new InitialContext();
        return (EntityManagerFactory) ctx.lookup("java:/comp/env/MyPersistenceUnitJNDI");
    }

    @Bean
    public TransactionManager transactionManager() throws Exception {
        InitialContext initCtx = new InitialContext();
        TransactionManager tm = (TransactionManager) initCtx.lookup("weblogic.transaction.TransactionManager");
        WebLogicJtaTransactionManager wlTx = new WebLogicJtaTransactionManager();
        wlTx.setTransactionManager(tm);
        return (TransactionManager) wlTx;
    }


    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        EclipseLinkJpaVendorAdapter va = new EclipseLinkJpaVendorAdapter();
        va.setDatabase(Database.ORACLE);
        return va;
    }

    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        LoadTimeWeaver ltw = new WebLogicLoadTimeWeaver();
        return ltw;
    }


}

Persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MyPersistenceUnit" transaction-type="JTA">

        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>db/myprojectdatasource</jta-data-source>

        <class>myproject.dao.entities.Person</class>

        <properties>
          <property name="eclipselink.target-server" value="WebLogic_10"/>
          <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

我的EJB类:

@Stateless(name="TestEJB", mappedName="ejb/MyProject/TestEJB")
@WebService
@Interceptors({SpringBeanAutowiringInterceptor.class})
@PersistenceUnit(name="MyPersistenceUnitJNDI", unitName="MyPersistenceUnit")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class TestEJB implements TestEJBRemote, TestEJBLocal {

    private static final Logger LOG = LoggerFactory.getLogger(TestEJB.class);

    @Autowired
    private ApplicationContext appContext;

    @Autowired
    private PersonRepository personRepo;


    public TestEJB() {
    }

    @WebMethod
    public String findPerson(String name) {
        return ""+ personRepo.findByName(name);
    }

    @WebMethod
    public String findAllPerson() {
        return ""+ personRepo.findAll();
    }


}

Person实体类(通过Eclipse自动生成):

@Entity
@Table(name="PERSON")
public class Person implements Serializable {
    private static final Long serialVersionUID = 1L;

    @Id
    @Column(name="ID")
    private Long id;

    private String name;


    public Person() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

...

}

PersonRepository类:

@Eager
public interface PersonRepository extends JpaRepository<Person, Long> {

    List<Person> findByName(String name);

}

DataConader类:

@Service
public final class DataConverter implements Converter<Person, PersonDTO> {


    @Autowired
    private PersonRepository personRepository;


    public DataConverter() {
    }

    @Override
    public PersonDTO convert(Person p) {

    ...

    }
}

更新:遵循stacktrace,尤其是本部分:

 at weblogic.deployment.EntityManagerFactoryProxyImpl.invoke(EntityManagerFactoryProxyImpl.java:96)
    at com.sun.proxy.$Proxy59.createEntityManager(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:283)
    at com.sun.proxy.$Proxy160.createNamedQuery(Unknown Source)
    at org.springframework.data.jpa.repository.query.NamedQuery.<init>(NamedQuery.java:62)
    at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:113)

如果无状态bean的创建是由对Transactional方法的调用触发的,那么Spring似乎无法初始化JPA存储库!!!

实际上,如果我执行以下操作:1.在ejb上调用一个非事务性方法(@TransactionAt0013 teType. NEVER):此调用成功2.现在我确实调用epository.findByName:它起作用了!

因此,问题似乎在于,当有正在进行的CMT事务时,Spring JPA存储库无法注册其命名查询。

这听起来熟悉吗?也许有一种方法可以避免那些存储库在事务上下文中初始化?(我尝试了@Eager注释,但在这个上下文中没有改变任何东西)。

p、 另一个信息:切换到Hibernate 3.6.10。Final as JPA Provider是解决这个问题的一个方法。我在这方面做得越多,就越觉得Spring数据JPA和Eclipselink之间可能存在漏洞(我都尝试过Eclipselink 2.3(嵌入Weblogic)和2.5.2)。

共有1个答案

容阳焱
2023-03-14

我想添加我发现的解决方法:切换到Hibernate 3.6.10。最终,JPA提供商解决了这个问题。

此开关只需更改持续时间即可。xml文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MyPersistenceUnit" transaction-type="JTA">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>db/myprojectdatasource</jta-data-source>

        <class>myproject.dao.entities.Person</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

在Spring,您仍然可以从JNDI上下文和JtaTransactionManager中查找EntityManagerFactory

注意,我显式地忽略了设置JtaFactoryClass

<代码>

Hibernate属性:如果我这样做,由于某些原因,Weblogic事务管理器在执行事务之前禁用自动提交。

 类似资料:
  • 我有办法: 和每个内部事务的另一个bean: 当我第一次尝试保存时,我有20行DB。每次下一次保存,我得到+10行。名称具有约束。当我得到错误时,事务是提交而不是继续。每次保存后我都会等待98行。

  • 尝试使用executeUpdate()插入/update/delete时引发异常。Select query工作得很好。我已经尝试了以前在Stack-Overflow中提到的类似错误的所有建议。感谢任何指导。 环境:Websphere Liberty:17.0.0.2、Eclipselink 2.6.4、JPA 2.1 在Liberty server上启用的功能 [审核]CWWKF0012I:服务器

  • 我使用Grails2.3.3构建了一个成功的应用程序。 下面是PicturesNWSHR的域定义: -迈克

  • 我正在使用Spring“@Transactional”注释来处理事务。 但以防万一 insert1()-成功插入表A中的数据。 这些插入是ibatis内置函数,用于触发DB中的插入。我遇到了以下异常 事务不会回滚,即insert1(),insert2()不会回滚 请让我知道我错过了什么

  • 在我的spring服务中,我调用了两个spring数据存储库方法 现在我的查询与事务管理相关。就我所了解和看到的代码而言,spring存储库使用@Transactional为其方法启用了事务。对于select操作,它的readonly=true。 我对事务的理解是,当执行选择操作时,会创建一个事务,然后为保存操作创建另一个事务,因为对于选择操作,事务只读=true。 我希望在单个事务中执行读写操作

  • 17. 事务管理