【发布时间】:2014-09-09 11:02:44
【问题描述】:
我的应用程序中有多个线程 - 其中一个正在等待在数据库中的某些更改中得到通知。我的问题是,一旦我持久化某个对象并通知另一个线程 - 当它在数据库中查询自上次更改以来的新对象时,找不到持久化的对象。
@Transactional()
public void persistMyEntity() {
// persisting and notifying from first thread
Entity myEntity = new Entity();
em.persist(myEntity)
em.flush();
someOtherBean.notifyChange();
}
...
以下 bean 应该被唤醒:
public class SomeOtherBean {
public void notifyChange() {
currentThread.interrupt();
}
public void run() {
currentThread = Thread.currentThread();
while(true) {
...
try {
Thread.sleep(VERY_LONG_TIME);
} catch (InterrupedExcdeption e) {
// we don't care
}
findNewPersistedObjects();
// nothing is found
}
}
}
如果我重新启动线程,它会正确找到新对象。
编辑:拆分事务方法没有任何区别:
public void persistMyEntity() {
proxy(this).persistMyEntityInternal();
someOtherBean.notifyChange();
}
@Transactional
public void persistMyEntityInternal() {
// persisting and notifying from first thread
Entity myEntity = new Entity();
em.persist(myEntity)
em.flush();
}
...
【问题讨论】:
标签: java multithreading hibernate