u010559943 2015-04-02 03:26 采纳率: 0%
浏览 8938

c3p0 连接不释放 请路过的大神过来看看

配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />  <!-- 打开Spring的Annotation支持 -->
<!-- 设定Spring 去哪些包中找Annotation -->
<context:component-scan base-package="com.myd" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${driverClass}" />
    <property name="jdbcUrl" value="${jdbcUrl}" />
    <property name="user" value="${username}" />
    <property name="password" value="${password}" />
    <!-- 配置连接池的初始值 -->
    <property name="acquireIncrement" value="${acquireIncrement}" />
    <property name="maxPoolSize" value="${maxPoolSize}" />
    <property name="minPoolSize" value="${minPoolSize}" />
    <property name="initialPoolSize" value="${initialPoolSize}" />
    <property name="maxIdleTime" value="${maxIdleTime}" />
    <property name="checkoutTimeout" value="${checkoutTimeout}" />
    <property name="autoCommitOnClose" value="${autoCommitOnClose}"></property>
    <property name="acquireRetryDelay" value="${acquireRetryDelay}" />
    <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"></property>
    <property name="maxStatements" value="${maxStatements}"></property>
    <property name="numHelperThreads" value="${numHelperThreads}"></property> 
</bean>
<!-- 导入src目录下的c3p0.properties文件 -->
<context:property-placeholder location="classpath:c3p0.properties" />
<!--创建Spring的SessionFactory工厂 -->
<!-- 如果使用的是Annotation的方式,不能使用LocalSessionFactoryBean,而应该使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <!-- 注入数据源 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 设置Spring取那个包中查找相应的实体类 -->
    <property name="packagesToScan">
        <value>com.myd.entity</value>
    </property>
    <property name="hibernateProperties">
        <!-- <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> -->
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.autoReconnect">true</prop>
            <prop key="jdbc.use_scrollable_resultset">false</prop>
            <prop key="hibernate.connection.release_mode">after_statement</prop>
            <prop key="hibernate.c3p0.validate">true</prop>
            <prop key="hibernate.c3p0.idle_test_period">30</prop>
            <prop key="hibernate.c3p0.timeout">30</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>  
        </props>
    </property>
</bean>
<!-- 开启HibernateTemplate,并且为其注入SessionFactory 使用HibernateTemplate不太方便的就是要获取session得通过getSessionFactory()方法获取 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置Spring的事务处理 -->
<!-- 创建事务管理器 -->
<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置AOP,Spring是通过AOP来进行事务管理的 -->
<aop:config>
    <!-- 设置pointCut表示哪些方法要加入事务处理 -->
    <!-- 以下的事务是声明在DAO中,但是通常都会在Service来处理多个业务对象逻辑的关系,注入删除,更新等,此时如果在执行了一个步骤之后抛出异常 
        就会导致数据不完整,所以事务不应该在DAO层处理,而应该在service,这也就是Spring所提供的一个非常方便的工具,声明式事务 -->
    <aop:pointcut id="allMethods"
        expression="execution(* com.myd.service.*.*(..))" />
    <!-- 通过advisor来确定具体要加入事务控制的方法 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" />
</aop:config>
<!-- 配置哪些方法要加入事务控制 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <!-- 让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 -->
        <tx:method name="*" propagation="REQUIRED" read-only="true" />
        <!-- 以下方法都是可能设计修改的方法,就无法设置为只读 -->
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<bean id="taskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="1000" />
    <property name="keepAliveSeconds" value="60" />
</bean>

这是事务
package com.myd.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.myd.dao.BaseDao;

/**

  • 可以考虑把所有公共的方法都写在baseDAo中,让所有的DAO都继承BaseDao
  • 这样基本上就实现了大量的基础方法,如果DAO中有逻辑处理特殊的方法,再在具体的实现类中的DAO中创建
  • @author Administrator
  • @param
    /
    @Repository("baseDaoImpl")
    public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {
    /
    *

    • 此处不能使用setSessionFactory注入,因为setSessionFactory在HibernateDaoSupport
    • 中已经定义了而且还是final的,不能被覆盖
    • @param sessionFactory */ @Resource(name = "sessionFactory") public void setSuperSessionFactory(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); }

    /**

    • 创建Class的对象来获取泛型的class */ private Class clz;

    @SuppressWarnings("unchecked")
    public Class getClz() {

    if (clz == null) {
        // 获取泛型的Class对象
        clz = (Class<Object>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }
    return clz;
    

    }

    public void add(T t) {
    getHibernateTemplate().getSessionFactory().openSession();
    getHibernateTemplate().save(t);
    getHibernateTemplate().getSessionFactory().close();
    }

    public void delete(int id) {
    getHibernateTemplate().getSessionFactory().openSession();
    getHibernateTemplate().delete(load(id));
    getHibernateTemplate().getSessionFactory().close();
    }

    @SuppressWarnings("deprecation")
    public void update(T t) {
    try {
    getHibernateTemplate().getSessionFactory().openSession()
    .connection().prepareStatement("SET SQL_SAFE_UPDATES=0")
    .execute();
    } catch (DataAccessResourceFailureException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (HibernateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    getHibernateTemplate().update(t);
    getHibernateTemplate().getSessionFactory().close();
    }

现在的问题是 运行一段时间后,就卡起, 不是报连接数据库关闭 就是 返回空指针
查了以下是连接未释放导致的,我想请教下各位大神 我这样连接没关闭并回到连接池吗?

  • 写回答

3条回答 默认 最新

  • u010559943 2015-04-02 07:32
    关注

    org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection

    是报这个错 你这2个连接 我都看了一下 先谢谢你

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试