在Spring 3.1中配置JDO?

hle*_*one 7 java spring jdo

我曾经让我的所有DAO扩展了JdoDaoSupport类,现在在Spring 3.1中已经弃用了.我已经创建了自己的AbstractJdoDao类,包装了PersistenceManagerFactory所有DAO并从那里扩展.这是我应该做的方式吗?

另外在JDO文档中,似乎直接实例化PersistenceManagerFactory不是默认选项,而是使用LocalPersistenceManagerFactoryBean包装在一个TransactionAwarePersistenceManagerFactoryProxy.如何正确地实例化这些bean并使它们与Spring的@Transactional注释一起使用.

这是我的应用程序上下文中与持久性相关的部分:

<bean id="persistenceManagerFactoryProxy" class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
    <property name="targetPersistenceManagerFactory">
        <bean class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
            <property name="jdoPropertyMap">
                <props>
                    <prop key="javax.jdo.PersistenceManagerFactoryClass">org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory</prop>
                    <prop key="javax.jdo.option.ConnectionURL">appengine</prop>
                    <prop key="javax.jdo.option.NontransactionalRead">true</prop>
                    <prop key="javax.jdo.option.NontransactionalWrite">false</prop>
                    <prop key="javax.jdo.option.RetainValues">false</prop>
                    <prop key="javax.jdo.option.DetachAllOnCommit">true</prop>
                    <prop key="javax.jdo.option.Multithreaded">true</prop>
                    <prop key="datanucleus.appengine.ignorableMetaDataBehavior">NONE</prop>
                </props>
            </property>
        </bean>
    </property>
    <property name="allowCreate" value="false" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
    <property name="persistenceManagerFactory" ref="persistenceManagerFactoryProxy" />
</bean>
Run Code Online (Sandbox Code Playgroud)

现在,当我加载访问数据存储的页面时:

org.springframework.transaction.CannotCreateTransactionException: Could not open JDO PersistenceManager for transaction; nested exception is java.lang.IllegalStateException: No JDO PersistenceManager bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.jdo.JdoTransactionManager.doBegin(JdoTransactionManager.java:369) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at $Proxy15.queryAll(Unknown Source) ~[na:na]
    ...
Caused by: java.lang.IllegalStateException: No JDO PersistenceManager bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.jdo.PersistenceManagerFactoryUtils.doGetPersistenceManager(PersistenceManagerFactoryUtils.java:153) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy$PersistenceManagerFactoryInvocationHandler.invoke(TransactionAwarePersistenceManagerFactoryProxy.java:159) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at $Proxy13.getPersistenceManager(Unknown Source) ~[na:na]
    at org.springframework.orm.jdo.JdoTransactionManager.doBegin(JdoTransactionManager.java:308) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
... 73 common frames omitted
Run Code Online (Sandbox Code Playgroud)

在GitHub上我的示例项目.它使用的是Google App Engine,因此要么通过mvn gae:runEclipse(使用Google Plugin for Eclipse)运行它,首先要创建一个Eclipse项目mvn eclipse:eclipse.

jmo*_*253 3

我的建议是使用TransactionAwarePersistenceManagerFactoryProxySpringPersistenceManagerProxyBean按照 Spring 3.1 文档的建议。看来这是为了取代 JdoDaoSupport 类而设计的。

虽然您在创建自己的AbstractJdoDao包装器的问题中所建议的当然会消除弃用警告,但我唯一担心的是您可能会无意中创建一种其他人难以维护的情况,因为这不会是他们习惯的情况看到。

另一方面,我想创建自己的包装器是解决问题的一种非常快速的方法......

您应该仔细权衡使用自己的包装器的优点/缺点以及继续使用 Spring 3.1 做事方式的优点/缺点。根据我的经验,走捷径可能而且经常会在未来困扰你。