贴主要部分代码
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; }
提示:org.hibernate.TransactionException: nested transactions not supported
而后删除Transaction tx那一行,提示:org.hibernate.SessionException:
Session is closed
尽管在applicationContext.xml那里配置了连接池,初始化为20条连接,但似乎并没效果。
getHibernateTemplate()可参考http://blog.csdn.net/zmhinzaghi/article/details/7091629
个人案例,请谅解。
时间: 2024-10-14 17:19:19