衍生线程中的 Spring 事务
我在现有系统上工作,需要将我的 Spring 事务管理器附加到从主作业线程生成的线程。我不断收到此异常,
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at parity.model.datamapping.RefreshTask.execute(RefreshTask.java:95)
at com.paritysys.tasks.ASyncTask$1.run(ASyncTask.java:48)
at java.lang.Thread.run(Thread.java:662)
我用事务注释注释了我能想到的所有内容,但我仍然无法清除此错误。
我缺少什么
方法
@TransactionAS400
@TransactionScores
public void refresh() {
ASyncTaskWorker worker = new ASyncTaskWorker() {
public void progress(double percentage, String message) {
logger.debug(String.format("%.2f - %s", percentage, message));
}
public void handleException(Throwable e) {
try {
status.allowUpdating(property.getSchema());
} catch (Exception b) {
// consume
}
logger.error("Failed to complete the refresh task", e);
}
public void done(boolean success) {
status.allowUpdating(property.getSchema());
logger.debug(String.format(
"Reset updating status to default for %s",
property.getSchema()));
}
};
RefreshTask refTask = ServiceProviderContext.find(RefreshTask.class);
refTask.init(property, worker);
ASyncTaskHandler.getInstance().add(refTask);
ASyncTaskHandler.getInstance().process();
}
im working on an existing system and need to have my spring transaction manager attach to a thread thats being spawned off of a master job thread. i keep getting this exception
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at parity.model.datamapping.RefreshTask.execute(RefreshTask.java:95)
at com.paritysys.tasks.ASyncTask$1.run(ASyncTask.java:48)
at java.lang.Thread.run(Thread.java:662)
ive annotated everything i can think of with the transactional annotation and i still cant get this error to clear.
what am i missing
my method
@TransactionAS400
@TransactionScores
public void refresh() {
ASyncTaskWorker worker = new ASyncTaskWorker() {
public void progress(double percentage, String message) {
logger.debug(String.format("%.2f - %s", percentage, message));
}
public void handleException(Throwable e) {
try {
status.allowUpdating(property.getSchema());
} catch (Exception b) {
// consume
}
logger.error("Failed to complete the refresh task", e);
}
public void done(boolean success) {
status.allowUpdating(property.getSchema());
logger.debug(String.format(
"Reset updating status to default for %s",
property.getSchema()));
}
};
RefreshTask refTask = ServiceProviderContext.find(RefreshTask.class);
refTask.init(property, worker);
ASyncTaskHandler.getInstance().add(refTask);
ASyncTaskHandler.getInstance().process();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的情况,我有一个线程超出了 Spring 的方面范围。
我的建议如下:
定义并连接
org.springframework.transaction.support.TransactionTemplate
的实例,然后使用:我建议事先调用
transactionTemplate.setName("myTransactionsName")
进行调试问题。I ran into similar situations where I have a thread out of Spring's aspects reach.
What I suggest is the following:
Define and wire an instance of a
org.springframework.transaction.support.TransactionTemplate
and then use:I'd recommend calling
transactionTemplate.setName("myTransactionsName")
beforehand for debugging issues.虽然另一个答案中建议的 TransactionTemplate 可以正常工作,但还有其他两个选项需要注意:
1)将方法定义为
@Transactional
(我不确定您在哪里使用其他注释来自)和
或声明一个DefaultAdvisorAutoProxyCreator
bean2) 定义事务
和切入点。两者均记录在 Spring 手册。
While a TransactionTemplate as suggested in another answer would work fine, there are two other options to be aware of:
1) Define the method as
@Transactional
(I'm not sure where those other annotations you are using are coming from) and<tx:annotation-driven>
or declare aDefaultAdvisorAutoProxyCreator
bean2) Define transactions with
<tx:advice>
and pointcuts.Both are documented in the Spring manual.