Some ideas and usages of spring management session

Source: Internet
Author: User
Tags ssh
In project development, session is used in multiple places when using the SSH framework integration. In the use of the session maintenance, there are several views:
1.getCurrentSession ();
Gets the session in the current conversation, which is managed by the container itself, and spring can broker the transaction.
2.this.getsession ();
To obtain or create a Hibernate session object from the current execution, you need to manually close it to free up connection resources.
3.openSession ();
The calling function creates a connection to a database itself and opens it, and in the case of a query that uses spring to manipulate a non-queried statement, the spring transaction does not play a role in transaction management for the session object, so the session object should be closed by the programmer itself, releasing the connection resources.

The above three kinds of operation session way, we are inseparable from this operation Main line:

Open the session, start a transaction, handle the exception, commit a transaction, and finally close a session.

If we are only focused on the business and do not want to do these repetitive and cumbersome operations, then such a set of operations is too cumbersome for us.

Believe that any programmer doesn't want to repeat the same code over and over again.

In the spring framework, hibernatetemplate wraps the Hibernate code and provides quite a few useful accessibility features. Spring's hibernatetemplate can help us do this, using declarative configuration to implement this functionality. Now that you've used SSH integration, take advantage of this feature of spring, and if you're worried about whether Spring has closed the session, look at this article: " One thing a programmer who looks a bit 2 but not 2 does: Verify that Hibernatetemplate has shut down session:http://songzj.iteye.com/blog/766148 "

But if we have to use the session to do business without having to deal with things, that means we're giving up all the benefits we've said.

Spring, she can still help us complete this kind of BT demand.


Hibernatetemplate provides hibernatecallback, in order to meet this use of the hibernatetemplate situation, still need to directly access the requirements of the session. It provides the ability to access the session directly within Hibernatetemplate, and programmers do not have to artificially manage the session object (this is the reason for using Hibernatecallback http://blog.163.com/ yeyhan/blog/static/44179333201022942856795/). First, you should make sure that our DAO has inherited Hibernatedaosupport.

So, if we can refer to the following form of writing:

@SuppressWarnings ("unchecked") public Page querydata (final string countsql, final string resultsql, final int startind EX, final int pageSize, final long total} {page result = (page) gethibernatetemplate (). Execute (New Hibernatecallba CK () {public Object doinhibernate throws Hibernateexception, SQLException {long total
						Count = 0;
							if (Total < 0) {List countlist = Session.createsqlquery (countsql). List ();
						TotalCount + + long.parselong (countlist.get (0). toString ());
						else {totalcount = total;
						} if (TotalCount < 1) {return new Page (); //The actual query returns the paging object query query = session. Createsqlquery (Resultsql). Addscalar ("Carlsh", Hiberna Te. INTEGER). Addscalar ("Carpic", hibernate.string). Addscalar ("Carinfo", hibernate.string). addscalar
						("Carcolor", hibernate.string). Addscalar ("Cartype", hibernate.string)		. Setresulttransformer (Transformers.aliastobean (Carinfo.class));
							if (PageSize > 0) {if (StartIndex < 0) {query.setfirstresult (0);
							else if (StartIndex >= totalcount) {return new Page ();
							else {query.setfirstresult (startIndex); } if (StartIndex + pageSize > TotalCount) {query.setmaxresults (int) Totalcount-startindex
							);
							else {query.setmaxresults (pageSize);
					} Return to New Page (StartIndex, TotalCount, pageSize, query. List ());
		}
				});
	return result; }





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.