In spring integrated hibernate, two operations are provided for Dao layer access.
Session org. springframework. Orm. hibernate3.support. hibernatedaosupport. getsession () throws dataaccessresourcefailureexception, illegalstateexception
Session org. springframework. Orm. hibernate3.support. hibernatedaosupport. getsession () throws dataaccessresourcefailureexception, illegalstateexception
Which method is used in the DaO layer?
In spring2.5 API Doc
"In general, it is recommended to use hibernatetemplate, either with the provided convenience operations or with a custom hibernatecallback that provides you with a session to work on. hibernatetemplate will care for all resource management and for proper exception conversion."
We recommend that you use "hibernatetemplate ".
Additional post
Spring + hibernate Data Access Policy
Source: http://blog.csdn.net/fenixshadow/archive/2007/09/26/1802277.aspx
Both spring and Hibernate are out of the box. I have a little bit of experience today. Share it with you.
When we spent a lot of effort to finally complete the environment, there was a problem:
How to access the database?
By inheriting hibernatedaosupport, we have two options:
Getsession (). createquery ("from users ");
Gethibernatetemplate (). Find ("from users );
Which one is used? Confused.
Gethibernatetemplate is recommended for finding information online. The reason is not clear.
So I did the following test:
Call getsession (). createquery ("from users"); gethibernatetemplate (). Find ("from users) cyclically );
1000 times
As a result, the getsession () package will soon fail to establish a connection. However, gethibernatetemplate cannot be used up.
Through the background observation, using getsession will leave a lot of SQL * Net message from client connections in the database. After the test is terminated, the connection is automatically released.
Gethibernatetemplate uses a connection from start to end.
Does getsession () Fail to automatically release the connection?
So I cyclically call getsession (). createquery ("from users"); gethibernatetemplate (). Find ("from users );
5 times
When front-endProgramWhen the process ends, the five connections of the getsession are immediately released. If the connection is automatically released after the previous 1000 tests are terminated, it means that getsession () will automatically release the connection.
Conclusion:
1. Both getsession () and gethibernatetemplate can automatically release connections (of course, your configuration must be correct ), however, getsession in a thread will get many sessions (that is, many sessions and connections), which may cause the database connection to exceed the upper limit. Therefore, gethibernatetemplate is recommended.
2. If gethibernatetemplateis useless, you can use gethibernatetemplate.exe cute to use the hibernatecallback callback interface.
In addition, you can set the allowcreate value of hibernatetemplate to true and disable the session in finally. You can also pass true as a parameter to the super. getsession (...) method to get the session. This is also possible, that is, the trouble.
See:
Http://springframework.org/docs/api/org/springframework/orm/hibernate3/HibernateTemplate.html
Http://www.mxjava.com/blog/article.asp? Id = 246
Please give us some advice.
In addition, hibernatedao automatically generated by myeclipseCode. In version 4.1.1, The findbyid method automatically generated by myeclipse uses the getsession method to obtain the connection. However, in version 6.0, it has been modified to use the gethibernatetemplate method. 5.0 is not tested.