给出以下Spring持久性配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager"
p:persistenceManagerFactory-ref="localPersistenceManagerFactoryBean" />
<bean id="transactionAwarePersistenceManagerFactoryProxy"
class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy"
p:targetPersistenceManagerFactory-ref="localPersistenceManagerFactoryBean"
p:allowCreate="false" />
<bean id="localPersistenceManagerFactoryBean"
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>
<prop key="datanucleus.appengine.autoCreateDatastoreTxns">true</prop>
</props>
</property>
</bean>
我有一个简单的服务,例如:
@Service
@Transactional
public class EntityServiceImpl implements EntityService {
@Autowired
private EntityRepository entityRepository;
@Override
public void create(Entity entity) {
entityRepository.create(entity);
}
}
以下是底层存储库:
@Repository
public class EntityAppEngineRepositoryImpl extends BaseAppEngineTxManager
implements EntityRepository {
@Override
public void create(Entity entity) {
final PersistenceManager pm = getPersistenceManager();
pm.makePersistent(entity);
}
}
当我调用我的服务方法来创建实体时,我得到以下异常:
org.datanucleus.jdo.exceptions.TransactionNotWritableException: Impossible to perform operation since it either requires an active transaction or 'NontransactionalRead' enabled. You have neither of these.
at org.datanucleus.jdo.JDOPersistenceManager.assertWritable(JDOPersistenceManager.java:2149)
at org.datanucleus.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:687)
at org.springframework.samples.mvc.basic.services.impl.EntityAppEngineRepositoryImpl.create(EntityAppEngineRepositoryImpl.java:25)
at org.springframework.samples.mvc.basic.services.impl.EntityServiceImpl.create(EntityServiceImpl.java:25)
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:616)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy19.create(Unknown Source)
at org.springframework.samples.mvc.basic.boot.Bootstrap.createEntity(Bootstrap.java:31)
at org.springframework.samples.mvc.basic.boot.Bootstrap.onApplicationEvent(Bootstrap.java:20)
at org.springframework.samples.mvc.basic.boot.Bootstrap.onApplicationEvent(Bootstrap.java:1)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:224)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:196)
at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:239)
at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:146)
at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:189)
at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
at com.google.appengine.tools.development.DevAppServerMain.<init>(DevAppServerMain.java:128)
at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:104)
enter code here
知道为什么我的交易不活跃?
谢谢!
答案 0 :(得分:0)
谁说交易不活跃?您将NontransactionalWrite设置为false,然后尝试在没有事务的情况下进行写入。因此你得到一个例外。 QED
您还使用古老版本的GAE JDO和DataNucleus。不支持DataNucleus版本。升级到最新
答案 1 :(得分:0)
您可能必须告诉spring根据方法名称为您创建事务。我确定@Transactional应该可以工作,但请使用@Transaction config检查以下内容:
<aop:config>
<!-- http://blog.espenberntsen.net/tag/pointcut/ -->
<!-- For all the classes annotated with @Service -->
<aop:pointcut id="serviceMethodsCut" expression="within(@org.springframework.stereotype.Service *)" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethodsCut" />
</aop:config>
<aop:config>
<!-- For all the methods annotated with @Transactional -->
<aop:pointcut id="transactionalCut" expression="execution(@org.springframework.transaction.annotation.Transactional * *(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionalCut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="create*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="store*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<bean id="transactionAwarePersistenceManagerFactoryProxy"
class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy"
p:targetPersistenceManagerFactory-ref="pmf"
p:allowCreate="true" />