简体   繁体   English

我应该如何在休眠状态下管理会话和事务?

[英]How should i manage Sessions and Transactions in hibernate?

I have a function that has some nested if statements and a query in each of the if statements ,how should i manage transaction and session calls.I know that i should bind each call with seperate transaction . 我有一个函数,其中包含一些嵌套的if语句和每个if语句中的查询,我应该如何管理事务和会话调用。我知道我应该将每个调用与单独的事务绑定。

Session session=null;
Transaction tx=null;
if(!list.isEmpty()){
    session=factory.openSession();
    tx=session.beginTransaction();
    session.createQuery(" update Tbluser set loggedStatus='9'").executeUpdate();
    tx.commit();
    session.close();
    if(list.get(1)=="N")
    {
        ///some query
    }
}

should i have not closed the session just yet or should i create a new Session object for the new query that i have? 我应该尚未关闭会话还是应该为新查询创建一个新的Session对象? or what else should be the correct approach? 还是其他正确方法?

If all the operations in the if blocks are related and atomic, you should consider opening one session and transaction before the first if block and commit/close after the last if block. 如果if块中的所有操作都是相关且原子的,则应考虑在第一个if块之前打开一个会话和事务,并在最后一个if块之后提交/关闭。

If each operations is separate, then you can open one session before the first if and enclose each if in a new transaction. 如果每个操作是分开的,则可以在第一个if之前打开一个会话,并将每个if包含在新事务中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM