getHiberanteTemplate, getCurrentSession and OpenSession



The use of getCurrentSession () to create the Session will be bound to the current thread to the use of OpenSession () is not.

The use of getCurrentSession () to create the Session in the commit or rollback will automatically shut down after using OpenSession () must be closed manually.

The use of getCurrentSession () need to add configuration file Hibernate.cfg.xml configuration is as follows:

If it is a local matter, and a database JDBC:

<propety name="Hibernate.current_session_context_class"> thread </ propety>

If it is the overall situation of things, and things jta, multiple database resources or things resources:

<propety name="Hibernate.current_session_context_class"> jta </ propety>

GetHiberanteTemplate the use of spring do not need to consider the management and the issue of closed session:

public List getEntityCriteria (final DetachedCriteria detachedCriteria) (
return (List) getHibernateTemplate (). executeFind (
new HibernateCallback () (
public Object doInHibernate (Session session)
throws HibernateException (
Criteria criteria = detachedCriteria
. getExecutableCriteria (session);
return criteria.list ();
)
));
)
public List getEntityCriteriaByPage (final DetachedCriteria detachedCriteria, final Page page) (
return (List) getHibernateTemplate (). executeFind (
new HibernateCallback () (
public Object doInHibernate (Session session)
throws HibernateException (
Criteria criteria = detachedCriteria
. getExecutableCriteria (session);

criteria.setFirstResult (page.getFirstItemPos ());
criteria.setMaxResults (page.getPageSize ());
return criteria.list ();
)
));
)

DetachedCriteria detachedCriteria = DetachedCriteria.forClass (Department.class);

detachedCriteria.add (Restrictions.eq ( "eid", seacher.getEid ()));// business condition id query

detachedCriteria.add (Restrictions.in ( "dprtid", ids ));// inquiries departments under the id array

detachedCriteria.add (Restrictions.like ( "name", "%" + seacher.getName () + "%"));// the name of fuzzy query

getEntityCriteriaByPage (detachedCriteria, pageinfo);

After checking connections activated for 0, was always used to connect to 1 (should be for their own test)

BasicDataSource newDataSource = (BasicDataSource) Global._ctx
. getBean ( "datasource_" + hibernateEntityName);
newDataSource.getNumActive ();
newDataSource.getInitialSize ();
newDataSource.getMaxActive ();
newDataSource.getMaxIdle ();
newDataSource.getNumIdle ();