首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

struts2.1+Hibernate3.3+spring3.0调整添加代理事务出错

2012-08-14 
struts2.1+Hibernate3.3+spring3.0整合添加代理事务出错做了一个整合的小例子,开始没有添加代理事务运行正

struts2.1+Hibernate3.3+spring3.0整合添加代理事务出错
做了一个整合的小例子,开始没有添加代理事务运行正常。后来我把代理事务加上,运行时报错了:

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 code
<?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:
Java code
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整合,很多地方不明白,贴的代码有些多,麻烦点耐心看下帮忙找出原因。谢谢大家了。


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

热点排行
Bad Request.