我如何找到 javax.transaction.RollbackException 的原因?

我在Java 6 上运行的websphere 8.5.5.4上使用Hibernate 4.2.21


数据库:Microsoft SQL Server 2012


有时,当我尝试使用以下代码更新实体时:


this.transaction.begin();

// lots of lines of code here before the merge

merge(carentrypermitrequest);

this.transaction.commit(); // exception here

这就是我获得会话工厂的方式:


protected SessionFactory getSessionFactory() {

        try {

            return (SessionFactory) new InitialContext().lookup("SessionFactory");

        } catch (Exception e) {

            log.error("Could not locate SessionFactory in JNDI", e);

            throw new IllegalStateException("Could not locate SessionFactory in JNDI");

        }

    }

这就是我获得交易的方式:


public final UserTransaction transaction = getUserTransaction();


protected UserTransaction getUserTransaction() {

        try {

            return (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");

        } catch (NamingException e) {

            e.printStackTrace();

            System.err.println("Could not locate UserTransaction in JNDI" + e.getMessage());

            throw new IllegalStateException("Could not locate UserTransaction in JNDI");

        }

    }

这是合并方法:


public CarEntryPermitRequest merge(CarEntryPermitRequest detachedInstance) {

            try {

                CarEntryPermitRequest result = (CarEntryPermitRequest) sessionFactory.getCurrentSession()

                        .merge(detachedInstance);

                return result;

            } catch (RuntimeException re) {

                throw re;

            }

        }


陪伴而非守候
浏览 277回答 3
3回答

慕森王

过去,我一直在为同样的问题苦苦挣扎:我试图用一些新数据更新一个实体,并不断地接收它RollBackException而没有任何痕迹。在调试 Hibernate 的源代码数小时后,我发现之前从 Hibernate 的源代码中抛出了另一个完全不同的异常,并吞入了一个空catch块并从任何外部视图中消失。这打破了EntityManager我用来合并我的数据的。特别是,Hibernate 试图实例化流程中所需的另一个实体,但找不到默认的公共构造函数。我catch仍然不知道Hibernate 的代码如何包含包含空块的代码行。我使用的版本是4.3.5.Final.我猜您遇到了同样的问题:一些异常,可能与我面临的异常(缺少公共默认构造函数)的性质不同,从休眠层抛出,在那里被捕获,然后无论如何都没有管理。我建议你操作相同的过程:下载 Hibernate 的源代码并在整个过程调用的方法中迭代其中,尤其要留意空的 catch 块。 请注意,在我的情况下,空的 catch 块不属于RollBackException!

吃鸡游戏

我认为您每次都必须初始化交易,但您在这一行中将交易声明为最终交易:public final UserTransaction transaction = getUserTransaction();因此,每次需要开始事务时,更改代码以根据您的连接对象生成新的事务对象。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java