【问题标题】:Spring Hibernate C3P0: Connections not releasedSpring Hibernate C3P0:连接未释放
【发布时间】:2017-06-06 15:47:58
【问题描述】:

C3P0 在事务完成后没有释放连接。 这是堆栈跟踪:

java.lang.Exception:调试堆栈跟踪:过期资源签出堆栈跟踪。 在 com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:555) 在 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755) 在 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682) 在 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140) 在 org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) 在 org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386) 在 org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:87) 在 org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:112) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl $5.doPrepare(StatementPreparerImpl.java:146) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) 在 org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)

....... 在 com.ikoda.dao.user.UserDaoImpl.getUserChildByEmailPassword(UserDaoImpl.java:329)

....................... 在 com.ikoda.service.springtransactions.AdminUserServiceImpl.doUserChildLoginByEmail(AdminUserServiceImpl.java:100) 在 com.ikoda.service.springtransactions.AdminUserServiceImpl.doLoginByEmail(AdminUserServiceImpl.java:89)

......................

调用的服务方法如下:

    @Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public User doLoginByEmail(String email, String password) throws UserStructureException
{
    User u = doUserMainLoginByEmail(email, password);
    if (null != u)
    {
        return u;
    }
    else
    {
        return doUserChildLoginByEmail(email, password);
    }
}




private UserMain doUserMainLoginByEmail(String email, String password) throws UserStructureException
{
    try
    {

        UserMain returnedUser = userDao.getUserMainByEmailPassword(email, password, DeliveryPropertiesSingleton.getInstance().getBusinessId());

        if (null != returnedUser && returnedUser.getPassword().equals(password))
        {

            return returnedUser;
        }
        return null;
    }
    catch (IndexOutOfBoundsException ioe)
    {
      ...
    }
    catch (Exception e)
    {
        ...
    }

}


private  UserChild doUserChildLoginByEmail(String email, String password) throws UserStructureException
{
    try
    {


        UserChild returnedUser = userDao.getUserChildByEmailPassword(email, password, DeliveryPropertiesSingleton.getInstance().getBusinessId());

        if (null != returnedUser && returnedUser.getPassword().equals(password))
        {

            return returnedUser;
        }
        return null;
    }
    catch (IndexOutOfBoundsException ioe)
    {
        ...
    }
    catch (Exception e)
    {
       ....
    }

}

dao方法如下:

@Transactional(propagation = Propagation.MANDATORY)
public UserMain getUserMainByEmailPassword(String email, String password, Long businessId)
{

    Session session = this.sessionFactory1.getCurrentSession();


    List<UserMain> result = session.createQuery("from UserMain where email like '" + email + "' and password like '" + password + "' and  businessid =" + businessId).list();

    if(result.size()==1)
    {
            UserMain u = result.get(0);

            return u;
    }
    return null;
}


@Transactional(propagation = Propagation.MANDATORY)
public UserChild getUserChildByEmailPassword(String email, String password, Long businessId)
{

    Session session = this.sessionFactory1.openSession();

    List<UserChild> result = session.createQuery("from UserChild where email like '" + email
            + "' and password like '" + password + "' and  businessid =" + businessId).list();

    if(result.size()==1)
    {
        UserChild u = result.get(0);

        return u;
    }
    return null;

}

矿池配置和交易配置如下:

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="org.postgresql.Driver" />
		<property name="jdbcUrl" value="jdbc:postgresql://localhost/xxx?useUnicode=true&#38;characterEncoding=utf8" />
		<property name="user" value="xxx" />
		<property name="password" value="xxxxxxx" />

		<property name="acquireIncrement" value="2" />
		<property name="minPoolSize" value="3" />
		<property name="maxPoolSize" value="100" />
		<property name="maxIdleTime" value="600" />
		<property name="unreturnedConnectionTimeout" value="45" />
		<property name="debugUnreturnedConnectionStackTraces" value="true" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory1" />

	</bean>

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

任何建议将不胜感激

【问题讨论】:

  • c3p0 告诉您您的应用程序没有释放连接。它正在帮助您找到一个应用程序问题,看起来您已经找到了。 (耶!)

标签: hibernate c3p0 spring-transactions


【解决方案1】:

好的,非常简单。

DAO 方法使用了需要显式关闭的 openSession。对于spring/hibernate事务管理的持久化,应该是getCurrentSession

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 2019-10-02
    • 1970-01-01
    相关资源
    最近更新 更多