我正在尝试设置以下组件:
TransactionRequiredException
:
javax.persistence.TransactionRequiredException:
Exception Description: No transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:113)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:1776)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:780)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy439.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy439.flush(Unknown Source)
at backend.dao.StoredObjectDAO.save(StoredObjectDAO.java:39)
at backend.service.StorageService.setObjectForKey(StorageService.java:35)
at backend.controller.StoredObjectController.setObjectForKey(StoredObjectController.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)
事务管理器的设置如各种教程所述,甚至eclipse通过在亚麻布窗格旁边指示带有关于图标的切入点来识别aspectj编织。
因为我正在使用@Transactional
注释,所以我正确地将<tx:annotaion-driven />
设置为“on”。
但是,我仍然得到同样的例外。有没有人知道在使用这个图书馆时是否需要特定的设置? 请找到下面附带的所有必要文件。
感谢, 马努
最后离开了Spring并使用了另一个堆栈:
我必须继续,这个解决方案在那段时间更加务实。 也许有时候给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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:load-time-weaver />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<import resource="applicationContext-dataSource.xml" />
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform" />
<property name="showSql" value="true" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/backend" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="backend" transaction-type="JTA">
<!--
jta-data-source is only used for the IDE. Spring/Runtime uses the
data source from applicationContext-dataSource.
-->
<jta-data-source>jdbc/backend</jta-data-source>
<class>backend.model.StoredObject</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
@Repository
@Transactional
public class StoredObjectDAO implements IStoredObjectDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
@Transactional(readOnly = true)
public Collection<StoredObject> listAll() {
TypedQuery<StoredObject> query = entityManager.createQuery("select s from StoredObject s", StoredObject.class);
Collection<StoredObject> results = query.getResultList();
return results;
}
@Override
public void save(StoredObject storedObject) {
entityManager.persist(storedObject);
entityManager.flush(); // <---- throws Exception
}
}
答案 0 :(得分:5)
如果从服务类调用DAO方法,请避免注释DAO方法@Transactional。而是在事务上注释您的Service类方法。这背后的原因是,您可能需要在一个事务下调用多个DAO方法。如果所有DAO方法都是事务性的,这可能会导致问题。因此,如果您对服务方法进行事务性注释,则可以在一个事务下调用任意数量的DAO方法。
如果发生任何异常,如果您注释服务类事务,则将回滚整个事务。
答案 1 :(得分:4)
使用readonly
attribue和propagation
模式时缺少。
默认propagation
模式为REQUIRED
。这就是为什么,你的编码将假设如下,
@Transactional(readOnly = true, propagation=Propagation.REQUIRED)
public Collection<StoredObject> listAll() {
......
}
点:使用readonly
传播模式时,REQUIRED
连接将抛出异常。
答案:如果您想使用readonly
连接,propagation
模式将为SUPPORTS
。
@Transactional(readOnly = true, propagation=Propagation.SUPPORTS)
public Collection<StoredObject> listAll() {
......
}
答案 2 :(得分:2)
目前没有交易活动?意味着没有开始entityTransacton
你能试试吗
@Override
public void save(StoredObject storedObject) {
entityManager.getTransaction().begin();
entityManager.persist(storedObject);
entityManager.flush(); // <---- throws Exception
}
但是为开始和提交保留一个单独的方法。
答案 3 :(得分:2)
您是否需要持久性单元中的提供程序?
答案 4 :(得分:2)
根据Spring docs
<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in.
This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.
请验证,您已正确配置
答案 5 :(得分:2)
错误似乎是EclipseLink使用EntityTransactionWrapper而不是我期望的JTATransactionWrapper,如果配置为使用JTA,就像这篇文章中的情况一样: Spring + Eclipselink + JtaTransactionManager = javax.persistence.TransactionRequiredException
您可能希望验证正在使用的persistence.xml是否已标记为JTA,并且是您显示的那个,因为它可能是您的类路径上的另一个persistence.xml。
答案 6 :(得分:0)
如果你使用transaction-type =“JTA”,那么在web容器ex:Tomcat中,你需要使用JOTM来管理事务并在Tomcat和Spring中配置它。
此外,在文件persistence.xml中,您需要添加以下行:
<properties>
<property name="eclipselink.target-server" value="org.eclipse.persistence.transaction.jotm.JotmTransactionController"/>
</properties>