Service end new multi-threaded use hibernatesession Eliminate no session

Source: Internet
Author: User

The new thread needs to bind the hibernate session. Ability to use transactions and delay loading functions in new threads, otherwise the no session exception will be exposed.

The workaround:

New Runnable () {                @Override public                void Run () {//                  ----------binding session to the current thread------------                    Sessionfactory sessionfactory = (sessionfactory) applicationcontext.getbean ("Sessionfactory");                    Boolean participate = Concurrentutil.bindhibernatesessiontothread (sessionfactory);//                  --------- Your business---------------<pre name= "code" class= "java" >//                  ----------Close Session------------                                       Concurrentutil.closehibernatesessionfromthread (participate, sessionfactory);                }                            }

Bindhibernatesessiontothread Method:

    public static Boolean Bindhibernatesessiontothread (Sessionfactory sessionfactory) {        if ( Transactionsynchronizationmanager.hasresource (sessionfactory)) {            //Do not modify the Session:just set the Participate flag.            return true;        } else {            Session session = Sessionfactory.opensession ();            Session.setflushmode (flushmode.manual);            Sessionholder Sessionholder = new Sessionholder (session);            Transactionsynchronizationmanager.bindresource (Sessionfactory, Sessionholder);        }        return false;    }

Closehibernatesessionfromthread method

    public static void Closehibernatesessionfromthread (Boolean participate, Object sessionfactory) {        if (!participate) {            Sessionholder Sessionholder = (sessionholder) Transactionsynchronizationmanager.unbindresource ( Sessionfactory);            Sessionfactoryutils.closesession (Sessionholder.getsession ());        }    }

Transaction boundaries are controlled by AOP or transactional tags. The demo sample code is just a guarantee that a transactional method can get the session object from the current thread when needed.

Most of the above code is captured from spring's opensessioninviewfilter.

Service end new multi-threaded use hibernatesession Eliminate no session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.