我正在开始一个新的线程来做一些努力工作。所以我创建了一个像这样扩展Thread的Spring bean:
@Component
@Scope("prototype")
public class AtualizaMovimentacoesThread extends Thread{
@Inject
MovimentacoesService service;
@Override
public void run() {
movimentacoesService.atualizarEleEFilhos(...);
}
}
所以,MovimentacoesService bean做了很多工作。
@Component
@Service
public class MovimentacoesCastor implements MovimentacoesService {
@Override
@Transactional()
public void atualizarEleEFilhos(...) {
}
}
但是,当atualizarEleEFilhos执行时,我没有得到任何会话或会话被关闭的异常:
Exception in thread "Thread-20" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.opatecnologia.castor.domain.Lancamento.cobrancas, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:169)
at br.com.opatecnologia.castor.domain.Lancamento.getCobranca(Lancamento.java:277)
at br.com.opatecnologia.castor.domain.Lancamento.getDataVencimento(Lancamento.java:915)
at br.com.opatecnologia.castor.service.impl.MovimentacoesCastor.atualizarEleEFilhos(MovimentacoesCastor.java:518)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy112.atualizarEleEFilhos(Unknown Source)
at br.com.opatecnologia.castor.util.AtualizaMovimentacoesThread.run(AtualizaMovimentacoesThread.java:52)
Spring和Hibernate日志表示事务和会话已打开:
14:47:19,454 DEBUG [DefaultListableBeanFactory] Returning cached instance of singleton bean 'transactionManager'
14:47:19,454 DEBUG [JpaTransactionManager] Creating new transaction with name [br.com.opatecnologia.castor.service.impl.MovimentacoesCastor.atualizarEleEFilhos]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
14:47:19,454 DEBUG [SessionImpl ] opened session at timestamp: 15085180394
14:47:19,454 DEBUG [JpaTransactionManager] Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@7d218878] for JPA transaction
14:47:19,454 DEBUG [JDBCTransaction ] begin
14:47:19,454 DEBUG [ConnectionManager ] opening JDBC connection
14:47:19,454 DEBUG [JDBCTransaction ] current autocommit status: false
14:47:19,456 DEBUG [JpaTransactionManager] Initiating transaction rollback
14:47:19,456 DEBUG [JpaTransactionManager] Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@7d218878]
14:47:19,456 DEBUG [JDBCTransaction ] rollback
14:47:19,456 DEBUG [JDBCTransaction ] rolled back JDBC Connection
14:47:19,456 DEBUG [ConnectionManager ] aggressively releasing JDBC connection
14:47:19,456 DEBUG [ConnectionManager ] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
14:47:19,457 DEBUG [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@7d218878] after transaction
我错过了什么吗?
Ps:我在控制器中正常注入AtualizaMovimentacoesThread并调用start来执行线程。
答案 0 :(得分:0)
I found a solution.
I'm passing to atualizarEleEFilhos, an object loaded by hibernate, outside the thread, that explains LazyInitializationException
So, I simply reload the object and everything works now! :D