在Spring 3.1中配置JDO? [英] Configuring JDO in Spring 3.1?
问题描述
我曾经让我的所有DAO扩展 JdoDaoSupport
类,现在是在Spring 3.1中弃用。我已经创建了自己的 AbstractJdoDao
类,它包装了 PersistenceManagerFactory
,并且所有DAO都从那里扩展。这是我应该做的吗?
I used to have all my DAOs extend the JdoDaoSupport
class which is now deprecated in Spring 3.1. I've made my own AbstractJdoDao
class which wraps the PersistenceManagerFactory
and all the DAOs extend from there. Is that the way I should be doing?
同样在关于JDO的文档,似乎直接实例化 PersistenceManagerFactory
不是默认选项,而是使用包含在 TransactionAwarePersistenceManagerFactoryProxy
中的 LocalPersistenceManagerFactoryBean
。如何正确实例化这些bean并使它们与Spring的 @Transactional
注释一起使用。
Also in the documentation on JDO, it seems that the direct instantiation of PersistenceManagerFactory
is not the default option, but to use LocalPersistenceManagerFactoryBean
wrapped in a TransactionAwarePersistenceManagerFactoryProxy
. How to properly instantiate these beans and make them work with the Spring's @Transactional
annotations.
这是持久性相关的我的应用程序上下文的一部分:
Here's the persistence-related part of my application context:
<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>
现在当我加载访问数据存储的页面时:
Now when I load a page accessing the data store:
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,因此要么通过Eclipse中的 mvn gae:run
运行它(使用Google Plugin for Eclipse),首先通过 mvn eclipse:eclipse
。
I've got my example project on GitHub. It's using Google App Engine, so either run it via mvn gae:run
in Eclipse (with the Google Plugin for Eclipse), first creating an Eclipse project via mvn eclipse:eclipse
.
推荐答案
我的建议是使用如Spring 3.1文档所建议的,TransactionAwarePersistenceManagerFactoryProxy
或 SpringPersistenceManagerProxyBean
。这似乎是为了取代JdoDaoSupport类。
My suggestion would be to use TransactionAwarePersistenceManagerFactoryProxy
or SpringPersistenceManagerProxyBean
as suggested by the Spring 3.1 documentation. It seems that this is designed to replace the JdoDaoSupport class.
虽然您在创建自己的 AbstractJdoDao $的问题中提出了什么建议c $ c>包装器当然会消除弃用警告,我唯一担心的是你可能会无意中创建一个其他人难以维护的情况,因为它不会是他们习惯看到的情况。
While what you're suggesting in your question of creating your own AbstractJdoDao
wrapper will of course eliminate the deprecation warning, my only concern is that you may inadvertently create a situation that's hard for others to maintain as it won't be what they are used to seeing.
另一方面,我想创建自己的包装器是一种解决问题的快速方法...
On the other hand, I imagine creating your own wrapper is a very fast way to solve your problem...
你应该仔细称重使用自己的包装器的优点/缺点与使用Spring 3.1的服务方式前进的优点/缺点。根据我的经验,走捷径可以并且经常会在将来再次困扰你。
You should carefully weigh the advantages/disadvantages of using your own wrapper with the advantages/disadvantages of moving forward with the Spring 3.1 way of doing things. In my experience, taking shortcuts can and oftentimes do come back to haunt you in the future.
这篇关于在Spring 3.1中配置JDO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!