Hibernate transaction Management-hibernatetransactionmanager-the management of Hibernate session

Source: Internet
Author: User

Because of the SSH still stay at a relatively elementary level of application, today in the face of a doubt to toss a long time, the specific problem is this,

There is such a test method,

1      Public Static voidtest1 () {2ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml", "Daocontext.xml");3Mgrmanager mgr = (mgrmanager) ctx.getbean ("Mgrmanager");4list<empbean> Emps = mgr.getempsbymgr ("WebLogic");5          for(Empbean empbean:emps) {6 System.out.println (Empbean.getempname ());7         }8}

One of the Mgrmanager is a business class that provides a query based on the name of the employee function, the above method of execution is completely no problem,

And then there's the following test method,

     Public Static void test2 () {        new classpathxmlapplicationcontext ("Applicationcontext.xml", "Daocontext.xml"  );         = (EmployeeDAO) ctx.getbean ("EmployeeDAO");         = Empdao.findbyname ("Oracle");        System.out.println (Emp.getsalary ());    }

Test1 is to use the business class to query the database indirectly, get results, and TEST2 is directly query database through DAO, but Test2 always error said no database session.

The solution is that all beans are managed by spring, since spring injects sessionfactory into the business class, why is there no DAO class?

And then by hand in the test2 wrote the following code, found that can be tested,

     Public Static void test3 () {        new classpathxmlapplicationcontext ("Applicationcontext.xml", "Daocontext.xml"  );         = (sessionfactory) ctx.getbean ("Sessionfactory");         = sf.opensession ();        List<Employee> emps = Session.createquery ("Select E from Employee e where e.name = ' Oracle '"). List (); 
    }

that is, the problem is not that the spring container is not sessionfactory, but that sessionfactory is not open! So why is the sessionfaction in the test1 example open again?

Looked at the configuration file half-day, found that there is such a critical line of configuration,

<id= "Leepointcut"  expression= "Bean (Empmanager) | | Bean (Mgrmanager)) "/>

In addition, through the aspect-oriented programming principle, Spring provides transaction management for the DAO object's data operation through the AOP mechanism, and has the following configuration,

<Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager">    <!--Configuring verbose transaction semantics -    <tx:attributes>        <!--all methods that begin with get are read-only -        <Tx:methodname= "get*"read-only= "true" />        <!--Other methods use the default settings -        <Tx:methodname="*" />    </tx:attributes></Tx:advice><Aop:config><!--Configure a pointcut to match the execution of all methods of the Empmanager and Mgrmanager two beans -<Aop:pointcutID= "Leepointcut"expression= "Bean (Empmanager) | | Bean (Mgrmanager) | | Bean (EmployeeDAO) " /><!--specifies that the Txadvice transaction enhancement is applied at the Leepointcut pointcut -<Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Leepointcut" /></Aop:config>

The following configuration is used to configure the real transaction management class, it is through the above section configuration, the business class is associated with the enhanced processing, and through the following transaction management class for transaction management

<id= "TransactionManager"  class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "    p:sessionfactory-ref = "Sessionfactory" />

So the focus on the above TransactionManager this bean, through the query data, a preliminary understanding, Hibernatetransactionmanager This class provides sessionfactory management, in order to achieve data synchronization, The Open and close of the hibernate session are performed inside the Hibernatetransactionmanager, and the opened Hibernaate Sesion is associated to the current application Session, in application in the Getcurrentsession way to obtain the open Hibernate session, in order to solve some aspects of thread safety and synchronization problems.

Thus, as the above-mentioned section of the configuration transaction management only for the business class, that is, by default only the business class is used to open the session, thus can understand the above test2 why the "No open session" error.

For the above configuration, I made the following changes,

<id= "Testmanager"  class= "Service.impl.TestManagerImpl " P:empdao-ref= "EmployeeDAO"/>

Sure enough, the test2 will execute normally.

The above only involves very, very small problems, mainly the understanding of the Hibernatetransactionmanager is not deep enough to cause.

Hibernate transaction Management-hibernatetransactionmanager-the management of Hibernate 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.