我们有这个JPA / Spring设置,我们已经查看了许多关于同一错误的其他线程。我们似乎遵循了正确的方法。但是当我们在WebSphere中测试它时,我们会看到可怕的
javax.persistence.TransactionRequiredException: no transaction is in progress
我们知道我们可以在Spring事务中混合使用JPA和JDBCTemplate查询。但是我们需要一个Spring / JPA专家来查看我们的配置。
码
@PersistenceContext(unitName="pu1")
private EntityManager entityManager;
@Transactional("jpaTransactionManager")
public DeviceVO updatedDevStatus( Test test) {
entityManager.persist(test)
entityManager.flush();
}
JPA配置
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jndiDataSource"></property>
</bean>
<!-- JPA -->
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:config/persistence.xml" />
<property name="persistenceUnitName" value="pu1" />
<property name="dataSource" ref="jndiDataSource" />
<property name="packagesToScan" value="com.test.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> -->
<property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect" />
</bean>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="jpaTransactionManager"/>
微量
[14/08/15 14:48:53:689 BST] 0000002b SystemOut O DEBUG [WebContainer : 0] DataExceptions.<init>(25) | javax.persistence.TransactionRequiredException: no transaction is in progress
[14/08/15 14:48:53:672 BST] 0000002b SystemErr R javax.persistence.TransactionRequiredException: no transaction is in progress
[14/08/15 14:48:53:673 BST] 0000002b SystemErr R at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:993)
[14/08/15 14:48:53:673 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[14/08/15 14:48:53:673 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[14/08/15 14:48:53:674 BST] 0000002b SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[14/08/15 14:48:53:674 BST] 0000002b SystemErr R at java.lang.reflect.Method.invoke(Method.java:611)
[14/08/15 14:48:53:674 BST] 0000002b SystemErr R at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
[14/08/15 14:48:53:674 BST] 0000002b SystemErr R at $Proxy800.flush(Unknown Source)
[14/08/15 14:48:53:675 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[14/08/15 14:48:53:675 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[14/08/15 14:48:53:675 BST] 0000002b SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[14/08/15 14:48:53:675 BST] 0000002b SystemErr R at java.lang.reflect.Method.invoke(Method.java:611)
[14/08/15 14:48:53:676 BST] 0000002b SystemErr R at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:262)
[14/08/15 14:48:53:676 BST] 0000002b SystemErr R at $Proxy800.flush(Unknown Source)
[14/08/15 14:48:53:676 BST] 0000002b SystemErr R at com.hrg.storecard.portal.service.dao.impl.DeviceDaoImpl.updatedDevStatus(DeviceDaoImpl.java:206)
[14/08/15 14:48:53:676 BST] 0000002b SystemErr R at com.hrg.storecard.portal.service.business.impl.DeviceServiceImpl.updateDeviceStatus(DeviceServiceImpl.java:94)
[14/08/15 14:48:53:676 BST] 0000002b SystemErr R at com.hrg.storecard.portal.controller.ManageDeviceController.updateDeviceStatus(ManageDeviceController.java:133)
[14/08/15 14:48:53:677 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[14/08/15 14:48:53:677 BST] 0000002b SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[14/08/15 14:48:53:677 BST] 0000002b SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[14/08/15 14:48:53:677 BST] 0000002b SystemErr R at java.lang.reflect.Method.invoke(Method.java:611)
[14/08/15 14:48:53:677 BST] 0000002b SystemErr R at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
[14/08/15 14:48:53:678 BST] 0000002b SystemErr R at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
[14/08/15 14:48:53:678 BST] 0000002b SystemErr R at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
[14/08/15 14:48:53:678 BST] 0000002b SystemErr R at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
[14/08/15 14:48:53:679 BST] 0000002b SystemErr R at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
[14/08/15 14:48:53:679 BST] 0000002b SystemErr R at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
[14/08/15 14:48:53:680 BST] 0000002b SystemErr R at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
[14/08/15 14:48:53:680 BST] 0000002b SystemErr R at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
[14/08/15 14:48:53:680 BST] 0000002b SystemErr R at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
[14/08/15 14:48:53:680 BST] 0000002b SystemErr R at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:874)