标签:hibernate transactions session spring
贴主要部分代码
public PlUser findByUsernameAndPassword(String username, String password) { Session sx = this.getHibernateTemplate().getSessionFactory().openSession(); Transaction tx = sx.getTransaction(); tx.begin(); PlUser user = (PlUser) sx.createQuery("from PlUser u where u.username=? and u.password=?") .setString(0, username) .setString(1, password) .uniqueResult(); tx.commit(); sx.close(); return user; }
原先的代码:
public PlUser findByUsernameAndPassword(String username, String password) { Session sx = this.getHibernateTemplate().getSessionFactory().getCurrentSession(); Transaction tx = sx.getTransaction(); tx.begin(); PlUser user = (PlUser) sx.createQuery("from PlUser u where u.username=? and u.password=?") .setString(0, username) .setString(1, password) .uniqueResult(); tx.commit(); sx.close(); return user; }
而后删除Transaction tx那一行,提示:org.hibernate.SessionException: Session is closed
尽管在applicationContext.xml那里配置了连接池,初始化为20条连接,但似乎并没效果。
getHibernateTemplate()可参考http://blog.csdn.net/zmhinzaghi/article/details/7091629
个人案例,请谅解。
org.hibernate.TransactionException: nested transactions not supported
标签:hibernate transactions session spring
原文地址:http://blog.csdn.net/u010102162/article/details/42525367