为什么我的StatelessSession要求进行容器交易?

时间:2014-07-14 20:14:43

标签: java hibernate ejb

我有一个可运行的任务如下

@TransactionManagement(TransactionManagementType.BEAN)
@TransactionAttribute(TransactionAttributeType.NEVER)
public class Task implements Runnable {

    private SessionFactory sessionFactory;

    public Task(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void run() {
        StatelessSession session = sessionFactory.openStatelessSession();
        session.beginTransaction();
        // ... the rest is irelevant because beginTransaction launches an exception

SessionFactory是由bean提供的,但是当我尝试开始事务时,即使很难我将该类标记为TransactionAttributeType.NEVER,hibernate也会启动异常

org.hibernate.TransactionException: Could not register synchronization for container transaction

我试过了this,但我只遇到了另一个问题

org.hibernate.TransactionException: Could not find UserTransaction in JNDI [java:comp/UserTransaction]

1 个答案:

答案 0 :(得分:0)

无状态会话不需要事务,删除ant尝试的事务语句。您可以阅读更多here

  

无状态会话也不实现第一级缓存   与任何二级缓存交互,也不实现   事务性后写或自动脏检查,也不做   操作级联到关联的实例。