Spring和thread:找不到当前线程的Session

时间:2012-07-05 08:48:17

标签: multithreading spring hibernate

在webapp中我想为一个长进程运行一个异步线程,让客户端知道进度状态。 为此,我想使用这个抽象类:

public abstract class ThreadProvider implements Runnable{

    protected Thread thread;

    public boolean create() throws SystemException{
        if(thread!=null && thread.isAlive()) throw new SystemException("Il processo è già in esecuzione");
        thread = new Thread(this);
        thread.start();
        return true;
    }
}

这是我的意思:

@Service @Transactional
public class ChiusuraProvider extends ThreadProvider {

    private static Logger gdf = Logger.getLogger("gdf");
    private static Logger log = Logger.getLogger(ChiusuraProvider.class);

    protected Dao dao;
    protected CinetelProvider cinetelProvider;

    @Override
    public void run() {...}
}

一切正常:线程启动,似乎autowire工作......但是Hibernat没有找到任何会话(也许是因为我改变了线程)......如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:1)

@Transactional在这种情况下不起作用,请参阅10.5.1 Understanding the Spring Framework's declarative transaction implementation

您可以在TransactionTemplate方法中使用程序化事务管理(run),或将事务逻辑提取到单独的bean中并使其成为@Transactional