Exception in thread "Timer-0" org.springframework.dao.InvalidDataAccessApiUsageException: No transactional EntityManager available; nested exception is java.lang.IllegalStateException: No transactional EntityManager available
出现异常原因:在使用 Timer 时出现异常。具体出发条件,含事务的主方法内 使用Timer做延时执行某个函数(不影响主方法正常执行)
异常结果如下:
Exception in thread "Timer-0" org.springframework.dao.InvalidDataAccessApiUsageException: No transactional EntityManager available; nested exception is java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:387)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:525)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:672)
at com.newcapec.sd.bmw.dao.impl.BmwPatriarchMsgPushSetDaoImpl$$EnhancerBySpringCGLIB$$e8089026.getOneConfigInfo(<generated>)
at com.newcapec.sd.bmw.service.impl.BmwPatriarchMsgPushSetServiceImpl.getOneConfigInfo(BmwPatriarchMsgPushSetServiceImpl.java:18)
at com.newcapec.sd.bmw.service.impl.BmwPatriarchMsgPushSetServiceImpl$$FastClassBySpringCGLIB$$64f11a23.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:668)
at com.newcapec.sd.bmw.service.impl.BmwPatriarchMsgPushSetServiceImpl$$EnhancerBySpringCGLIB$$e478f6b2.getOneConfigInfo(<generated>)
at com.newcapec.sd.open.platform.service.impl.OpenAPIServiceImpl.disposePatriarchPushInfo(OpenAPIServiceImpl.java:906)
at com.newcapec.sd.open.platform.service.impl.OpenAPIServiceImpl.access$000(OpenAPIServiceImpl.java:94)
at com.newcapec.sd.open.platform.service.impl.OpenAPIServiceImpl$TaskTimer.run(OpenAPIServiceImpl.java:889)
at com.newcapec.sd.open.platform.service.impl.OpenAPIServiceImpl$1.run(OpenAPIServiceImpl.java:835)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
at com.sun.proxy.$Proxy141.unwrap(Unknown Source)
at com.newcapec.core.dao.AbstractDaoHibernate5.getSession(AbstractDaoHibernate5.java:68)
at com.newcapec.core.dao.AbstractDaoHibernate5.createQuery(AbstractDaoHibernate5.java:189)
at com.newcapec.core.dao.AbstractDaoHibernate5.queryByNameParam(AbstractDaoHibernate5.java:245)
at com.newcapec.core.dao.AbstractDaoHibernate5.getByProperty(AbstractDaoHibernate5.java:175)
at com.newcapec.core.dao.AbstractDaoHibernate5.getByPropertySql(AbstractDaoHibernate5.java:472)
at com.newcapec.sd.bmw.dao.impl.BmwPatriarchMsgPushSetDaoImpl.getOneConfigInfo(BmwPatriarchMsgPushSetDaoImpl.java:31)
at com.newcapec.sd.bmw.dao.impl.BmwPatriarchMsgPushSetDaoImpl$$FastClassBySpringCGLIB$$a7726c83.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:737)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 14 more
解决方案:通过异常关键字 “No transactional EntityManager available;” ,释义:没有可用的事务EntityManager; 我们需要做的就是在执行时加入事务管理,需要用到 TransactionTemplate 编程式事务 。
<pre>@Autowired
private TransactionTemplate transactionTemplate;//编程式控制事务= 手动事务</pre>
编程式事务可以点击查看:[深入理解TransactionTemplate编程式事务_.... 永远年轻,永远热泪盈眶-CSDN博客_transactiontemplateSpring可以支持编程式事务和声明式事务。Spring提供的最原始的事务管理方式是基于TransactionDefinition、PlatformTransactionManager、TransactionStatus 编程式事务。而TransactionTemplate的编程式事务管理是使用模板方法设计模式对原始事务管理方式的封装。源码探索查看 TransactionTempla...[图片上传失败...(image-61ebe7-1639558182448)]
https://blog.csdn.net/qq_33404395/article/details/83377382](https://blog.csdn.net/qq_33404395/article/details/83377382 "深入理解TransactionTemplate编程式事务_.... 永远年轻,永远热泪盈眶-CSDN博客_transactiontemplate")
@Autowired
private TransactionTemplate transactionTemplate;//编程式控制事务= 手动事务
@Transactional//注意:这个事务注解只针对主函数起作用
public void dispose(){
/*
主函数代码
*/
Timer timer = new Timer();
Long t = 30000L;
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("延时执行"+DateUtils.formatNow());
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
// do whatever you want to do with c1
//在这里进行你需要进行业务代码处理,操作数据库的查询等
return null;
}
});
}
},t);//30s
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39165437/article/details/121955271
————————————————
版权声明:本文为CSDN博主「A万小乐。」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39165437/article/details/121955271