2021年11月2日 | Leave a comment 问题: org.hibernate.LazyInitializationException: could not initialize proxy [articleindex.scheduler.SiteEntity#1] - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:170) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at articleindex.scheduler.SiteEntity$HibernateProxy$ekNyt81A.getDomain(Unknown Source) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.site(MySQLCrawleScheduler.java:289) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.page(MySQLCrawleScheduler.java:277) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$$Lambda$1299/0x000000009d26bac8.apply(Unknown Source) ~[na:na] at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na] at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:497) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:487) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na] at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:239) ~[na:na] at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na] at articleindex.scheduler.MySQLCrawleScheduler.listPages(MySQLCrawleScheduler.java:260) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.schedule(MySQLCrawleScheduler.java:91) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$TriggerTimerTask.lambda$run$0(MySQLCrawleScheduler.java:71) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$TriggerTimerTask$$Lambda$1280/0x000000009c9159d0.run(Unknown Source) ~[na:na] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na] at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na] at java.base/java.lang.Thread.run(Thread.java:853) ~[na:na] 12345678910111213141516171819202122232425 org.hibernate.LazyInitializationException: could not initialize proxy [articleindex.scheduler.SiteEntity#1] - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:170) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at articleindex.scheduler.SiteEntity$HibernateProxy$ekNyt81A.getDomain(Unknown Source) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.site(MySQLCrawleScheduler.java:289) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.page(MySQLCrawleScheduler.java:277) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$$Lambda$1299/0x000000009d26bac8.apply(Unknown Source) ~[na:na] at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na] at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:497) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:487) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na] at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:239) ~[na:na] at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na] at articleindex.scheduler.MySQLCrawleScheduler.listPages(MySQLCrawleScheduler.java:260) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler.schedule(MySQLCrawleScheduler.java:91) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$TriggerTimerTask.lambda$run$0(MySQLCrawleScheduler.java:71) ~[classes/:na] at articleindex.scheduler.MySQLCrawleScheduler$TriggerTimerTask$$Lambda$1280/0x000000009c9159d0.run(Unknown Source) ~[na:na] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na] at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na] at java.base/java.lang.Thread.run(Thread.java:853) ~[na:na] 解决: 避免在 timer 线程中处理,可以通过 EventListerner 切换到正常的线程中去执行 参考: