getCurrentSession hibernate in web

后端 未结 2 657
误落风尘
误落风尘 2021-02-01 10:41

I am writing a web based application with hibernate and jsp/servlet. I have read about the sessionFactory.getCurrentSession and sessionFactory.openSession

2条回答
  •  面向向阳花
    2021-02-01 11:00

    I don't have an answer for your scenario because I would not implement it that way as it seems to be asking for trouble. Instead, I'd start the transaction in C, where C invokes A and B, and have C issue the commit. Skeletally:

    public void c(...) {
        try {
           transaction.begin();
           a();
           b();
           transaction.commit();
        catch (Exception e) {
           transaction.rollback();
        }
    }
    

    So here, a() and b() do not commit or rollback - how do they know the entire business task has been completed? They could throw an exception or perhaps return a boolean to tell the caller that something is amiss and a rollback is needed.

提交回复
热议问题