我在@Service bean中有下一个方法:
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void save (String lsk) {
Session sess = (Session)em.getDelegate(); <--Error!
有时(随机)会抛出错误异常:
--- [nio-8080-exec-1] o.s.t.i.TransactionInterceptor : Application exception overridden by rollback exception
java.lang.NullPointerException: null
at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:235) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.internal.SessionImpl.internalClear(SessionImpl.java:395) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.internal.SessionImpl.clear(SessionImpl.java:391) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at com.ric.bill.ChrgServ.save(ChrgServ.java:227) ~[classes/:na]
at com.ric.bill.ChrgServ$$FastClassBySpringCGLIB$$67dcba28.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) [spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at com.ric.bill.ChrgServ$$EnhancerBySpringCGLIB$$b101e62c.save(<generated>) [classes/:na]
at com.ric.bill.BillServ.chrgHouse(BillServ.java:121) [classes/:na]
at com.ric.bill.BillServ.chrgAll(BillServ.java:88) [classes/:na]
at com.ric.web.GreetingController.greeting(GreetingController.java:81) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
为什么会发生,我不知道。
我使用Hibernate 5.1.0.Final,Java 1.8,Spring framework 4.2.5.RELEASE, Oracle 11 G
任何帮助都会很棒。
UPD:
@Service
public class ChrgServ {
@Autowired
private ApplicationContext ctx;
@PersistenceContext
private EntityManager em;