Using StatelessSession for Batch processing

后端 未结 3 1034
孤街浪徒
孤街浪徒 2020-12-03 03:39

From documentation

If we have a case where we need to insert 1000 000 rows/objects:

Session session = sessionFactory.openSession();
Transaction tx =         


        
3条回答
  •  日久生厌
    2020-12-03 04:06

    Stateless Session has an advantage over Session in terms of performance because stateless session will skip the transaction commit to session or session flush methods used in Session object. However, it is important to note that the service/DAO should NOT try to perform in-session data manipulation to either parent or any child object. It will throw exception. Also, make sure to close the session explicitly otherwise one will end up with leaked connections.

    To gain more performance with Stateless session, if one is using Spring driven transaction, mark the Spring transaction as read only and set the propagation required as NEVER.

    But again, do not try this where one has to manipulate the object model.

    @Transactional(value="someTxnManager", readOnly=true, propagation=Propagation.NEVER)
        public List get(...) {
    
            return daoSupport.get(...);
        }
    

    in daoSupport

    StatelessSession session = sessionFactory.openStatelessSession();
    try{
    // do all operations here
    }
    ...
    ...
    finally{
                session.close();
    }
    

提交回复
热议问题