在Spring 3.1中配置JDO?

我曾经让我所有的DAO扩展JdoDaoSupport类,现在在Spring 3.1中已经弃用了它。 我已经创建了自己的AbstractJdoDao类,它包装了PersistenceManagerFactory并且所有的DAO都从那里延伸出来。 这是我应该怎么做的方式?

同样在JDO的文档中,似乎PersistenceManagerFactory的直接实例化不是默认选项,而是使用包装在TransactionAwarePersistenceManagerFactoryProxy LocalPersistenceManagerFactoryBean 。 如何正确地实例化这些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>

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

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

我在GitHub上有我的示例项目。 它使用Google App Engine,因此可以通过mvn gae:run运行它mvn gae:run在Eclipse中mvn gae:run (使用Google Plugin for Eclipse),首先通过mvn eclipse:eclipse创建一个Eclipse项目mvn eclipse:eclipse


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

虽然你在创建自己的AbstractJdoDao包装器时提出的建议当然会消除弃用警告,但我唯一担心的是,您可能无意中创建了一个很难让其他人维护的情况,因为它不会被使用去看。

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

你应该仔细权衡使用自己的包装器的优点/缺点,以及推进Spring 3.1的做法的优点/缺点。 根据我的经验,采取捷径可能而且往往会在未来困扰着你。

链接地址: http://www.djcxy.com/p/10347.html

上一篇: Configuring JDO in Spring 3.1?

下一篇: How does the c# compiler concat strings