数据核:从@Transactional变为非事务性 [英] Datanucleus: moving from @Transactional to non-transactional

查看:121
本文介绍了数据核:从@Transactional变为非事务性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Datanucleus,JDO和与Aspect-J结合使用的Spring声明式@Transactional管理.

I am using Datanucleus, JDO and Spring's declarative @Transactional management woven with Aspect-J.

但是,当普通"方法从@Transactional方法获取持久对象时,该对象的状态将变为瞬态(似乎删除了持久性管理器),并且该对象不再持久.

But when a 'normal' method gets a persistent object from a @Transactional method, the object's state will become transient (the persistence manager seems to be removed) and the object is no longer persistent.

示例:

public class Example {

    public void test() throws Exception {
       Login l = getLogin();                          
       JDOHelper.getObjectState(l);              // transient instead of persistent
       l.getSomeOtherPersistentObj().doStuff();  // NullpointerException :(
    }

    @Transactional
    private Login getLogin() {
        // do transactional stuff here
        // ..
        return dao.find(Login.class, 1);
    }
}

这是为什么?如何在不需要事务的地方不添加@Transactional的情况下解决该问题?下面的(显然)确实起作用,因此表明可以建立事务连接和非事务连接:

Why is this and how can I fix it without adding @Transactional in places where transactions are not needed? The following does (obviously) work so this indicates that a transactional as well as a non-transactional connection can be made:

  • @Transactional方法调用@Transactional方法
  • @Transactional方法调用普通方法
  • 普通方法调用普通方法

如果我调用dao.refresh(l),则会得到:'id为"的对象是由其他对象管理器管理的,所以也许Spring正在使用与DAO不同的持久性管理器,这是原因吗?

If I call dao.refresh(l), I get: 'Object with id "" is managed by a different Object Manager', so maybe Spring is working on a different persistence manager than the DAO, is this the cause?

这是我的弹簧配置(可能与之相关):

Here's my spring configuration (it might be relevant):

<bean id="pmf" class="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" destroy-method="close">
  <property name="connectionDriverName" value="com.mysql.jdbc.Driver"/>
  ...
  <constructor-arg>
    <map>
      <entry key="datanucleus.autoCreateSchema" value="true" />
    </map>
  </constructor-arg>
</bean>
<bean id="myPmfProxy" class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
  <property name="targetPersistenceManagerFactory" ref="pmf" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
  <property name="persistenceManagerFactory" ref="myPmfProxy" /> 
</bean>
<bean id="JDODao" class="sw.JDODao">
  <property name="persistenceManagerFactory" ref="myPmfProxy" /> 
</bean>

推荐答案

原来,我的对象需要可拆卸才能做到这一点.

It turned out that my objects need to be detachable to do this.

已将(detachable="true")添加到我的@PersistenceCapable批注中,并设置了以下数据核选项:

Iv'e added (detachable="true") to my @PersistenceCapable annotations and set the following datanucleus options:

<entry key="datanucleus.DetachAllOnCommit" value="true" />
<entry key="datanucleus.detachedState" value="all" />

这篇关于数据核:从@Transactional变为非事务性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆