首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

HibernateDaoSupport getSession()不能释放资源的有关问题及解决办法

2012-09-07 
HibernateDaoSupport getSession()不能释放资源的问题及解决方法??? 根据代理机制的不同,总结了五种Spring

HibernateDaoSupport getSession()不能释放资源的问题及解决方法


??? 根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

??? 第一种方式:每个Bean都有一个代理

  1. <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? ????xmlns:context="http://www.springframework.org/schema/context"??
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"?? ????xsi:schemaLocation="http://www.springframework.org/schema/beans? ??
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ?? ???????????http://www.springframework.org/schema/context ??
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ?? ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">??
  6. ?? ????<bean?id="sessionFactory"?? ??
  7. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">?? ?? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>?? ??
  8. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>?? ????</bean>?? ??
  9. ?? ????<!--?定义事务管理器(声明式的事务)?-->?? ??
  10. ????<bean?id="transactionManager"?? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">??
  11. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?? ????</bean>??
  12. ???? ?? ????<!--?配置DAO?-->??
  13. ????<bean?id="userDaoTarget"?class="com.bluesky.spring.dao.UserDaoImpl">?? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>??
  14. ????</bean>?? ???? ??
  15. ????<bean?id="userDao"?? ?? ????????class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">?? ??
  16. ???????????<!--?配置事务管理器?-->?? ?? ???????????<property?name="transactionManager"?ref="transactionManager"?/>????? ??
  17. ????????<property?name="target"?ref="userDaoTarget"?/>?? ?? ?????????<property?name="proxyInterfaces"?value="com.bluesky.spring.dao.GeneratorDao"?/>??
  18. ????????<!--?配置事务属性?-->?? ?? ????????<property?name="transactionAttributes">?? ??
  19. ????????????<props>?? ?? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>??
  20. ????????????</props>?? ?? ????????</property>?? ??
  21. ????</bean>?? ?? </beans>??

??? 第二种方式:所有Bean共享一个代理基类

  1. <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? ????xmlns:context="http://www.springframework.org/schema/context"??
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"?? ????xsi:schemaLocation="http://www.springframework.org/schema/beans? ??
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ?? ???????????http://www.springframework.org/schema/context ??
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ?? ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">??
  6. ?? ????<bean?id="sessionFactory"?? ??
  7. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">?? ?? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>?? ??
  8. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>?? ????</bean>?? ??
  9. ?? ????<!--?定义事务管理器(声明式的事务)?-->?? ??
  10. ????<bean?id="transactionManager"?? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">??
  11. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?? ????</bean>??
  12. ???? ?? ????<bean?id="transactionBase"?? ??
  13. ????????????class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"?? ?? ????????????lazy-init="true"?abstract="true">?? ??
  14. ????????<!--?配置事务管理器?-->?? ?? ????????<property?name="transactionManager"?ref="transactionManager"?/>?? ??
  15. ????????<!--?配置事务属性?-->?? ?? ????????<property?name="transactionAttributes">?? ??
  16. ????????????<props>?? ?? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>?? ??
  17. ????????????</props>?? ?? ????????</property>?? ??
  18. ????</bean>???? ?? ??? ??
  19. ????<!--?配置DAO?-->?? ????<bean?id="userDaoTarget"?class="com.bluesky.spring.dao.UserDaoImpl">??
  20. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?? ????</bean>??
  21. ???? ?? ????<bean?id="userDao"?parent="transactionBase"?>?? ??
  22. ????????<property?name="target"?ref="userDaoTarget"?/>??? ?? ????</bean>??
  23. </beans>?? ??

第三种方式:使用拦截器

  1. <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? ????xmlns:context="http://www.springframework.org/schema/context"??
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"?? ????xsi:schemaLocation="http://www.springframework.org/schema/beans? ??
  4. ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ?? ???????????http://www.springframework.org/schema/context ??
  5. ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ?? ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">??
  6. ?? ????<bean?id="sessionFactory"?? ??
  7. ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">?? ?? ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>?? ??
  8. ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>?? ????</bean>?? ??
  9. ?? ????<!--?定义事务管理器(声明式的事务)?-->?? ??
  10. ????<bean?id="transactionManager"?? ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">??
  11. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>?? ????</bean>? ??
  12. ??? ?? ????<bean?id="transactionInterceptor"?? ??
  13. ????????class="org.springframework.transaction.interceptor.TransactionInterceptor">?? ?? ????????<property?name="transactionManager"?ref="transactionManager"?/>?? ??
  14. ????????<!--?配置事务属性?-->?? ?? ????????<property?name="transactionAttributes">?? ??
  15. ????????????<props>?? ?? ????????????????<prop?key="*">PROPAGATION_REQUIRED</prop>?? ??
  16. ????????????</props>?? ?? ????????</property>?? ??
  17. ????</bean>?? ?????? ??
  18. ????<bean?class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">?? ?? ????????<property?name="beanNames">?? ??
  19. ????????????<list>?? ?? ????????????????<value>*Dao</value>??
  20. ????????????</list>?? ?? ????????</property>?? ??
  21. ????????<property?name="interceptorNames">?? ?? ????????????<list>?? ??
  22. ????????????????<value>transactionInterceptor</value>?? ?? ????????????</list>?? ??
  23. ????????</property>?? ?? ????</bean>?? ??
  24. ?? ?? ????<!--?配置DAO?-->??
  25. ????<bean?id="userDao"?class="com.bluesky.spring.dao.UserDaoImpl">?? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>??
  26. ????</bean>?? </beans>??
  27. ??

第四种方式:使用tx标签配置的拦截器

  1. <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? ????xmlns:context="http://www.springframework.org/schema/context"??
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"?? ????xmlns:tx="http://www.springframework.org/schema/tx"??
  4. ????xsi:schemaLocation="http://www.springframework.org/schema/beans? ?? ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ??
  5. ???????????http://www.springframework.org/schema/context ?? ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ??
  6. ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ?? ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">??
  7. ?? ????<context:annotation-config?/>??
  8. ????<context:component-scan?base-package="com.bluesky"?/>?? ??
  9. ????<bean?id="sessionFactory"?? ?? ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">?? ??
  10. ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>?? ?? ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>??
  11. ????</bean>?? ?? ??
  12. ????<!--?定义事务管理器(声明式的事务)?-->?? ?? ????<bean?id="transactionManager"??
  13. ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">?? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>??
  14. ????</bean>?? ??
  15. ????<tx:advice?id="txAdvice"?transaction-manager="transactionManager">?? ????????<tx:attributes>??
  16. ????????????<tx:method?name="*"?propagation="REQUIRED"?/>?? ????????</tx:attributes>??
  17. ????</tx:advice>?? ???? ??
  18. ????<aop:config>?? ????????<aop:pointcut?id="interceptorPointCuts"??
  19. ????????????expression="execution(*?com.bluesky.spring.dao.*.*(..))"?/>?? ????????<aop:advisor?advice-ref="txAdvice"??
  20. ????????????pointcut-ref="interceptorPointCuts"?/>???????? ?? ????</aop:config>?????? ??
  21. </beans>?? ??

第五种方式:全注解

  1. <?xml?version="1.0"?encoding="UTF-8"?>?? <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? ????xmlns:context="http://www.springframework.org/schema/context"??
  3. ????xmlns:aop="http://www.springframework.org/schema/aop"?? ????xmlns:tx="http://www.springframework.org/schema/tx"??
  4. ????xsi:schemaLocation="http://www.springframework.org/schema/beans? ?? ???????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ??
  5. ???????????http://www.springframework.org/schema/context ?? ???????????http://www.springframework.org/schema/context/spring-context-2.5.xsd ??
  6. ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ?? ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">??
  7. ?? ????<context:annotation-config?/>??
  8. ????<context:component-scan?base-package="com.bluesky"?/>?? ??
  9. ????<tx:annotation-driven?transaction-manager="transactionManager"/>?? ??
  10. ????<bean?id="sessionFactory"?? ?? ????????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">?? ??
  11. ????????<property?name="configLocation"?value="classpath:hibernate.cfg.xml"?/>?? ?? ????????<property?name="configurationClass"?value="org.hibernate.cfg.AnnotationConfiguration"?/>??
  12. ????</bean>?? ?? ??
  13. ????<!--?定义事务管理器(声明式的事务)?-->?? ?? ????<bean?id="transactionManager"??
  14. ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">?? ????????<property?name="sessionFactory"?ref="sessionFactory"?/>??
  15. ????</bean>?? ???? ??
  16. </beans>?? ??

此时在DAO上需加上@Transactional注解,如下:

  1. package?com.bluesky.spring.dao; ?? ??
  2. import?java.util.List; ?? ??
  3. import?org.hibernate.SessionFactory; ?? import?org.springframework.beans.factory.annotation.Autowired; ??
  4. import?org.springframework.orm.hibernate3.support.HibernateDaoSupport; ?? import?org.springframework.stereotype.Component; ??
  5. ?? import?com.bluesky.spring.domain.User; ??
  6. ?? @Transactional??
  7. @Component("userDao") ?? public?class?UserDaoImpl?extends?HibernateDaoSupport?implements?UserDao?{ ??
  8. ?? ????public?List<User>?listUsers()?{ ??
  9. ????????return?this.getSession().createQuery("from?User").list(); ?? ????} ??
  10. ???? ?? ???? ??
  11. } ?? ??
来源:http://blog.csdn.net/myloon/archive/2009/11/11/4798904.asp

热点排行