做了一个整合的小例子,开始没有添加代理事务运行正常。后来我把代理事务加上,运行时报错了: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:679)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback
.......
.......
Caused by: org.hibernate.SessionException: Session is closed
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:139)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:217)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:196)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:676)
... 74 more看错误提示说session关闭了,好我想到我的方法里是把session关闭了,于是注释掉,在运行,没有错,但是没有显示正确结果。
applicationContext.xml:<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306"></property>
<property name="username" value="root"></property>
<property name="password" value="lm"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>


<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 
<bean id="studentDAO" class="com.lm.Impdao.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="stuBiz" class="com.lm.Impbiz.StudentBiz">
    <property name="stuDAO" ref="studentDAO"></property>
</bean>

<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
   <property name="transactionManager" ref="tm"></property>
   <property name="transactionAttributes">
     <props>
         <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
     </props>
   </property>
</bean>

<bean id="stuBizProxy" parent="transactionProxy">
   <property name="target" ref="stuBiz"></property>
</bean>

<bean id="userAction" class="com.lm.struts2.userAction" scope="prototype">
  <property name="stuBiz" ref="stuBizProxy"></property>
</bean>

</beans>
StudentDAO:package com.lm.Impdao;import java.util.List;import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.lm.Idao.IStudentDAO;
import com.lm.dao.Student;
public class StudentDAO extends HibernateDaoSupport implements IStudentDAO{
private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
// property constants
public static final String STU_NAME = "stuName";
public static final String STU_PWD = "stuPwd";

protected void initDao() {

} public void save(Student transientInstance) {
log.debug("saving Student instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(int stuId) {
log.debug("deleting Student instance");
Session session=null;
try {
String hql="delete from Student where stuId="+stuId;
session=this.getSession();
Query query = session.createQuery(hql);

query.executeUpdate();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}finally{
//session.close();
}
}

public void deleteAll() {
log.debug("deleting Student instance");
Session session=null;
try {
String hql="delete  from Student";
session=this.getSession();
Query query = session.createQuery(hql);
query.executeUpdate();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}finally{
//session.close();
}
} public Student findById(java.lang.Integer id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getHibernateTemplate().get(
"com.lm.dao.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findAll() {
log.debug("finding all Student instances");
Session session=null;
try {
String hql="from Student order by stuId desc";
session=this.getSession();
Query query = session.createQuery(hql);
    return query.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}finally{
//session.close();
}
} public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
return (StudentDAO) ctx.getBean("studentDAO");
}
}
声明一下,这个例子不添加代理事务是能正常运行的,添加之后,就报session已关闭的异常,于是在代码里把session.close()注释掉没有再报异常,但是却返回了一个自定义的错误页面(说明没有得到正确的显示结果)。
我刚学SSH整合,很多地方不明白,贴的代码有些多,麻烦点耐心看下帮忙找出原因。谢谢大家了。

解决方案 »

  1.   

    session.close()肯定是不能够在用了,因为你用了spring的事务后,spring会在你事务完成后自动close的,所以不能加,加了就会异常,返回到了一个自定义的错误页面这个原因那就要你自己去分析了,StudentBiz要debug下
      

  2.   

    好了,问题解决了。
    第一点关于异常,就像一楼说的用了spring的事务后,spring会在你事务完成后自动close的,所以不能加,加了就会异常。
    第二点跳转到错误页面,是因为我的所有操作都是访问一个Action(即一个Action中有多个方法),而有的操作时要更改数据库中数据的,比如删除,修改,增加,有的事只读的,比如查询,而我的配置文件里全部设置成了只读,所以进行删除等操作会出错。在配置文件里把配置把
    <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
       <property name="transactionManager" ref="tm"></property>
       <property name="transactionAttributes">
         <props>
             <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
         </props>
       </property>
    </bean>改为
    <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
       <property name="transactionManager" ref="tm"></property>
       <property name="transactionAttributes">
         <props>
             <prop key="*">PROPAGATION_REQUIRED</prop>
         </props>
       </property>
    </bean>即去掉readOnly即可。