首先,我是spring的初学者。我创建了一个简单的Spring服务,它注入了DAO,事务由Spring的HibernateTransactionManager管理,如下所示。(并使用注释使用事务配置)
@Service(value="daopowered")
public class UserServiceImplDao implements UserService
{
@Inject
private UserDao userDao;
@Override
@Transactional
public User autheticate( String userId, String password )
{
return userDao.findByIdAndPassword(userId, password);
}
我的交易配置如下
<bean id="txMgr"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txMgr" />
现在问题是当我第一次使用某个控制器调用authenticate方法然后它工作正常(数据库操作成功)但是再次调用它后第二次休眠会话关闭异常即将到来?请指导我做错了什么或如何处理这种情况?当我第二次调用这个方法时,为什么春天不会打开新的交易?
异常追踪:
2013-05-22T21:04:18.041+0530 DEBUG [14208212-2] lerExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
2013-05-22T21:04:18.044+0530 DEBUG [14208212-2] tusExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
2013-05-22T21:04:18.044+0530 DEBUG [14208212-2] lerExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
2013-05-22T21:04:18.046+0530 DEBUG [14208212-2] et.DispatcherServlet Could not complete request
org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:658)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.convertHibernateAccessException(AbstractSessionFactoryBean.java:245)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.translateExceptionIfPossible(AbstractSessionFactoryBean.java:224)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy28.findByIdAndPassword(Unknown Source)
编辑:DAO代码
@Repository
public class UserDaoImpl extends GenericHibernateDAO<User, Long>
implements
UserDao
{
@Override
public User findByIdAndPassword( String id, String password )
{
Criteria crit = getSession().createCriteria(User.class).add(Restrictions.eq("userId",
id)).add(Restrictions.eq("password",
password)).setMaxResults(1);
List<?> list = crit.list();
if (list.size() > 0)
return (User) list.get(0);
else
return null;
}
和getSession()实现是
protected Session getSession() {
if (session == null) {
session = sessionFactory.getCurrentSession();
}
return session;
}
抽象的DAO类也注入了sessionfactory
public abstract class GenericHibernateDAO<T, ID extends Serializable>
implements GenericDAO<T, Long> {
private Class<T> persistentClass;
protected Session session;
@Autowired
private SessionFactory sessionFactory;
答案 0 :(得分:2)
你的ObjectDao需要一个SessionFactory和注释Transaction。像这样:
@Component
public class userDao{
@AutoWired
private SessionFactory sessionFactory;
@Transactional
public User findByIdAndPassword(String id , String password){
....
}
{getters and setters}
}
不要这样做:
protected Session getSession() {
if (session == null) {
session = sessionFactory.getCurrentSession();
}
return session;
}
只返回当前会话,Transactional注释负责打开和关闭会话,如下所示:
protected Session getSession() {
return sessionFactory.getCurrentSession();
}