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

Windows系统下复现PointNet++算法 本文详细介绍了如何在Windows系统下复现PointNet++算法,内容包括程序下载、环境配置、数据集准备以及三个主要任务(语义分割、分类、部件分割)的实现步骤和常见报错解决方法。通过遵循本文的指导,用户可以顺利完成PointNet++算法的复现。 阅读详情

 

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

 

1、使用手动释放资源:

Session session = getSession();

//代码

//Query query= session.createQuery(hql);

//query.list();

releaseSession(session);

另外还可以用getHibernateTemplate()来代替。

2、使用注解@Transactional管理事务

这个需要注解驱动:

<tx:annotation-driven transaction-manager="transactionManager"

                   proxy-target-class="true" />

在使用类或者方法上用@Transactional注解即可。

3、不使用注解来管理事务,使用声明式事务管理

         <tx:advice id="txAdvice" transaction-manager="transactionManager">

                   <tx:attributes>

                            <tx:method name="get*" read-only="true" />

                            <tx:method name="*" />

                   </tx:attributes>

         </tx:advice>

 

         <aop:config>

                   <aop:pointcut id="MyDaoOperation"

                            expression="execution(* x.y.dao.MyDAO.*(..))" />

                   <aop:advisor advice-ref="txAdvice" pointcut-ref="MyDaoOperation" />

         </aop:config>

或者

         <aop:config>

                   <aop:pointcut id="daoOperation"

                            expression="execution(* x.y.dao.*.*(..))" />

                   <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation" />

         </aop:config>

 

其他事务配置方法参考spring文档或

http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

4、添加openSessionInView,该方法没有亲自测试。

注:1、2、3种方法亲自测试过可以使用,最好的方法是配置spring事务,如果存在

HibernateDaoSupport getSession()连接占用问题,就要检查其spring事务管理的配置是否正确。

参考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

Spring事务配置的五种方式

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

    总结如下:

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

    DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问 时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。

    具体如下图:

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

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

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           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">


   
<bean id="sessionFactory" 
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
       
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
       
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

   
</bean> 

   
<!--
定义事务管理器(声明式的事务) --> 
   
<bean id="transactionManager"

        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
   
   
<!-- 配置DAO -->
   
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
   
   
<bean id="userDao" 
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 
          
<!--
配置事务管理器 --> 
          
<property name="transactionManager" ref="transactionManager" />
    
       
<property name="target" ref="userDaoTarget" />
 
        
<property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />

       
<!-- 配置事务属性 --> 
       
<property name="transactionAttributes">
 
           
<props>
 
               
<prop key="*">PROPAGATION_REQUIRED</prop>

           
</props> 
       
</property>
 
   
</bean>
 
</beans>

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

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           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">


   
<bean id="sessionFactory" 
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
       
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
       
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

   
</bean> 

   
<!--
定义事务管理器(声明式的事务) --> 
   
<bean id="transactionManager"

        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
   
   
<bean id="transactionBase" 
            class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
 
            lazy-init
="true" abstract="true">
 
       
<!--
配置事务管理器 --> 
       
<property name="transactionManager" ref="transactionManager" />
 
       
<!--
配置事务属性 --> 
       
<property name="transactionAttributes">
 
           
<props>
 
               
<prop key="*">PROPAGATION_REQUIRED</prop>
 
           
</props>
 
       
</property>
 
   
</bean>
   
  
   
<!--
配置DAO -->
   
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
   
   
<bean id="userDao" parent="transactionBase" > 
       
<property name="target" ref="userDaoTarget" />
  
   
</bean>

</beans>

第三种方式:使用拦截器

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           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">


   
<bean id="sessionFactory" 
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
       
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
       
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

   
</bean> 

   
<!--
定义事务管理器(声明式的事务) --> 
   
<bean id="transactionManager"

        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean> 
  
   
<bean id="transactionInterceptor"
 
        class
="org.springframework.transaction.interceptor.TransactionInterceptor">
 
       
<property name="transactionManager" ref="transactionManager" />
 
       
<!--
配置事务属性 --> 
       
<property name="transactionAttributes">
 
           
<props>
 
               
<prop key="*">PROPAGATION_REQUIRED</prop>
 
           
</props>
 
       
</property>
 
   
</bean>

     
   
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
       
<property name="beanNames">
 
           
<list>
 
               
<value>*Dao</value>

           
</list> 
       
</property>
 
       
<property name="interceptorNames">
 
           
<list>
 
               
<value>transactionInterceptor</value>
 
           
</list>
 
       
</property>
 
   
</bean>
 
 
   
<!--
配置DAO -->
   
<bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
</beans>

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

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


   
<context:annotation-config />
   
<context:component-scan base-package="com.bluesky" />

   
<bean id="sessionFactory" 
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
       
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
       
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

   
</bean> 

   
<!--
定义事务管理器(声明式的事务) --> 
   
<bean id="transactionManager"

        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>

   
<tx:advice id="txAdvice" transaction-manager="transactionManager">
       
<tx:attributes>
           
<tx:method name="*" propagation="REQUIRED" />
       
</tx:attributes>
   
</tx:advice>
   
   
<aop:config>
       
<aop:pointcut id="interceptorPointCuts"
            expression
="execution(* com.bluesky.spring.dao.*.*(..))" />
       
<aop:advisor advice-ref="txAdvice"
            pointcut-ref
="interceptorPointCuts" />       
   
</aop:config>
     
</beans>

第五种方式:全注解

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


   
<context:annotation-config />
   
<context:component-scan base-package="com.bluesky" />

   
<tx:annotation-driven transaction-manager="transactionManager"/>

   
<bean id="sessionFactory" 
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 
       
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
       
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

   
</bean> 

   
<!--
定义事务管理器(声明式的事务) --> 
   
<bean id="transactionManager"

        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
       
<property name="sessionFactory" ref="sessionFactory" />
   
</bean>
   
</beans>

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

package com.bluesky.spring.dao;

import
java.util.List;

import
org.hibernate.SessionFactory;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import
org.springframework.stereotype.Component;

import
com.bluesky.spring.domain.User;

@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements
UserDao {

   
public
List<User> listUsers() {
       
return this
.getSession().createQuery("from User").list();
    }
   
   

}

 

 

【七.三维视觉与视频分析】【1.点云处理:PointNet++/PointTransformer】 各位老铁,今天咱们要聊的可不是普通的二维图像处理,而是三维视觉领域最硬核的技术——点云处理。从最早的PointNet到如今的PointTransformer,这场技术进化堪比三维版的"像素革命"。今天咱们就深入解剖两大里程碑:PointNet++和PointTransformer,保证让你看完全文就能在点云领域横着走!想象一下,你抓了把沙子撒在空中,每粒沙子的位置都是三维坐标——这就是点云的典型形态。最后送大家一句话:点云处理就像在三维宇宙中寻找星辰的轨迹,既要望远镜的宏观视野,也要显微镜的细致入微。 阅读详情

相关推荐

共封装光学(CPO):下一代互联革命

Nvidia:产品:Quantum-X Q3450(115.2T,144×800G,2025H2上市)、Spectrum 6800(409.6T,512×800G,2026H2上市);技术路线:TSMC COUPE封装、MRM调制器、光栅耦合、外部激光源(Lumentum/Ayar Labs供应);战略定位:以横向扩展CPO验证供应链,最终聚焦纵向扩展场景(Feynman架构)。

存储随笔 5027

Hibernate数据连接不能正常释放的原因,以及在监听中获取apolicationContext上下文...

Hibernate数据库连接不能正常释放: https://blog.csdn.net/u011644423/article/details/44267301 监听中获取applicationContext上下文 https://blog.csdn.net/budapest/article/details/38493003 转载于:https://www.cnblogs.com/hmy-13...

weixin_30279751的博客 130

RAG在医药行业为什么80%都翻车了?

这篇文章不是要劝你别做RAG,而是把坑说清楚。医药行业有它的特殊性——监管压力、专业术语密度、多语言混排、文档结构复杂——这些因素叠加在一起,让标准RAG教程里的那套方法论几乎直接失效

qq_31808385的博客 637

hibernate默认连接池存在过期连接无法释放的问题及解决方法。

hibernate默认连接池有一个问题,不会自动检测数据库连接是否断开,MYSQL数据库一段时间(大约8小时)没有访问就会断开连接,连接池里的连接却还是存在,下次访问hibernate会继续使用这个连接,导致数据库连接异常。由于该问题需要在服务器长时间运行时才会出现,所以在平时测试很难发现。 解决方法: 1.在连接参数中使用autoReconnect以后,第一次执行失败后会自...

weixin_33711647的博客 1017

iOS 上架遭遇 App Store 4.3 拒审?Flutter 社交项目专项解决指南

在 iOS 应用开发与上架过程中,开发者常会遇到审核被拒的情况,其中 App Store 的 4.3 条款(Design Spam)因判定标准模糊且主观性强,成为众多团队头疼的难题。尤其是基于 Flutter 框架开发的社交类应用,由于二进制结构相对固定、功能同质化明显,更容易触发苹果审核系统的重复应用识别机制。理解 4.3 的底层逻辑,本质是解决产品差异化不足的问题,而不仅是修改代码层面缺陷。本文从审核系统的工作原理出发,分析机器扫描与人工审核的不同侧重点,系统梳理了从自审报告、工程身份重构、核心流程差异

weixin_33910385的博客 458

【STM32】从零构建串口IAP框架:BootLoader与App双向跳转实战解析

本文详细解析了STM32串口IAP框架的构建,重点阐述了BootLoader与App双向跳转的实战原理与实现步骤。内容涵盖Flash分区规划、中断向量表重定向、通信协议设计及固件升级流程,并针对跳转失败、程序跑飞等常见问题提供了解决方案,帮助开发者掌握产品级远程升级的核心技术。

wind6的博客 843

hibernate配置 持有数据库链接不释放

使用springmvc+hibernate做项目的时候,经常遇到一个问题项目动不动就假死啦,请求数据没有返回,debug模式下看到请求只要一连接数据库就会卡住,没有任何反应!刚开始什么方法都试过啦,调大数据库连接池的大小,优化请求链接的次数!查看假死情况下cpu的使用情况也没有异常,这个时候感觉很无力,后来同事换了一个数据库连接池Druid,之前用的是c3p0!终于有重大发现啦,这个时候再发生假死...

qq_25041295的博客 3361

攻防世界-Reverse-Game

攻防世界,game,逆向

wuh2333的博客 785

hibernate连接mysql 释放连接_hibernate 中关于数据库连接释放的问题(困扰好久的问题)...

以下是我用hibernate做的一个web应用的部分代码,appserver用的是tomcat5.5,现在的问题是应用运行一天到两天会报一个不能打开数据库连接的错误,我怀疑是数据库连接没有释放,大家帮我看看如何进行释放数据库连接数以及hibernate所申请的资源,如果需要的话我可以贴出其他代码。1.hibernate的初始化配置hibernate.cfg.xml/p>"-//Hiberna...

weixin_35949264的博客 468

安卓逆向实战:用Batch Apktool 3.8.0快速汉化APK的完整流程

本文详细解析了如何使用Batch Apktool 3.8.0进行安卓APK汉化的完整流程,包括环境配置、反编译、资源解析、深度汉化、重构建与签名优化等关键步骤。通过这款集成化逆向工具,开发者可以高效完成APK汉化,提升工作效率。

s1t2u3的博客 1119

YOLO11涨点优化:卷积魔改 | DCNv2升级版本,助力检测

在DCN的基础上,增加了2个创新点,分别是调制模块和使用多个调制后的DCN模块,从形成了DCN的升级版本——DCNv2

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 976

Hibernate分页查询频繁操作出现数据库连接池不释放

就我所知,SSH框架配置的开源数据库连接池有4种,分别是DBCP、C3P0、Proxool、BoneCP。工作中最先接触使用的是DBCP,之后又使用了C3P0。最近在使用hibernate分页功能时,发现重复频繁的使用分页查询时数据库连接会得不到释放!!!我的分页查询使用的语句是: Query query = getSession().createQuery(hql); Pager pager

dream_1086的专栏 1601

Rockchip eMMC寿命监控:SMART信息读取与预防性维护

擦写计数坏块数量温度使用时间。

小灰灰的博客 783

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

HibernateDaoSupport getSession()不能释放资源的问题及解决方法 核心提示:1、使用手动释放资源: Sessionsession=getSession(); //代码 //Queryquery=session.createQuery(hql); //query.list(); releaseSession(session); 另外还可以用getH...

weixin_33750452的博客 276

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

使用Spring的HibernateDaoSupport时的getSession()方法相关问题

在继承了HibernateDaoSupport的Dao中,  this.getsession实际上是调用了父类HibernateDaoSupport中的方法获得session。使用spring管理hibernate的SessionFactory的时候,这个方法会从session池中拿出一session。这样做有可能有问题,尽管这种方式拿到的Session会自动关闭,但是他是有一定的失效策略的

Seven's Rainbow Paradise 4317

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

使用Spring的HibernateDaoSupport时的getSession()3个方法的区别

在继承了HibernateDaoSupport的Dao中,  this.getsession实际上是调用了父类HibernateDaoSupport中的方法获得session。使用spring管理hibernate的SessionFactory的时候,这个方法会从session池中拿出一session。这样做有可能有问题,尽管这种方式拿到的Session会自动关闭,但是他是有一定的失效策

技术改变生活,生活改变思想,思想改变技术。 1万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

getHibernateTemplate()和getSession()的区别

自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1.使用getSession()方法你只要继承sessionFactory,而使用getHibernateTemplate()方法必须继承HibernateDaoSupport当然包括sessionFa

zeb_perfect的专栏 1003

Spring 的 HibernateDaoSupport 类的 getSession() 导致的连接泄露问题

Spring+Hibernate做项目, 发现有member在不加事务的情况下就去调用 getSession() 方法, 结果导致数据库连接不能释放, 也无法正常的提交事务(只能做查询, 不能做save(), update()). 如果配合连接池使用的话, 不出几分钟就会导致连接池无法拿到新连接的情况.   不过, 只要给DAO或者Service加入了事务, 就不会出现连接泄漏的问题.   谈

Seven's Rainbow Paradise 1948

HibernateDaoSupport getSession()不能释放资源

1、使用手动释放资源: Session session = getSession(); //代码 //Query query= session.createQuery(hql); //query.list(); releaseSession(session); 另外还可以用getHibernateTemplate()来代替。 2、使用注解@Transact...

terryjs 153
上一篇: Spring + toplink-essentials(JPA) is not a known entity type 问题解决
下一篇: 一份Axis2 webService及动态客户段调运的例子
「已注销」
博客等级 码龄20年 61粉丝 50原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值