如果仍然可以将Hibernate与数据库一起使用(和c3p0 dbpooling),并且可以安全地绕过Hibernate直接获取连接 [英] If using Hibernate with Database (and c3p0 dbpooling) is is still possible and safe to directly get connection bypassing Hibernate

查看:78
本文介绍了如果仍然可以将Hibernate与数据库一起使用(和c3p0 dbpooling),并且可以安全地绕过Hibernate直接获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在H2 1.4.199和C3p0上使用Hibernate 4.3.11

Using Hibernate 4.3.11 with H2 1.4.199 and C3p0

如果仍然可以在数据库中使用Hibernate,并且可以安全地绕过Hibernate直接从池中获取连接?

If using Hibernate with Database is is still possible and safe to directly get connection from pool bypassing Hibernate?

我正在尝试编写一个sql查询,现在我知道我可以使用session.createSQLQuery()了,但这会返回一个Hibernate org.hibernate SQLQuery类,而不是java.sql。 .Connection,这给我尝试设置参数带来了麻烦,因此我想尝试使用Connection。

Im trying to write an sql query, now I know I can use session.createSQLQuery() but this returns a Hibernate org.hibernate SQLQuery class rather than a java.sql.Connection and this is causing a problem for me trying to set the parameters, so I wanted to try using a Connection instead.

推荐答案

尝试一下。

import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;

sessionFactory.getSessionFactoryOptions().getServiceRegistry().getService(ConnectionProvider.class).getConnection();

我还没有测试过,目前无法做到。我在ConnectionProvider上只有50-50,因为我不确定c3p0的提供程序类是什么。可能是 org.hibernate.c3p0.internal.C3P0ConnectionProvider 或您已经知道。

I have not tested this and cannot do it at this time. I am only 50-50 on the ConnectionProvider, since I am not sure what the provider class for c3p0. May be org.hibernate.c3p0.internal.C3P0ConnectionProvider or you already know it.

编辑11 -10-2019

连接是从 org.hibernate.engine.jdbc.connections.spi.ConnectionProvider的实现返回的接口,据我所见。

The connections are returned from the implementations of org.hibernate.engine.jdbc.connections.spi.ConnectionProvider interface, as far as I can see.

org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl

两者这些实现从服务注册时内部维护的数据源或PooledConnections中获取 java.sql.Connection

Both these implementations are fetching a java.sql.Connection from Datasource or PooledConnections that are internally maintained at the time of service registry.

DatasourceConnectionProviderImpl 具有以下方法。

    @Override
    public Connection getConnection() throws SQLException {
        if ( !available ) {
            throw new HibernateException( "Provider is closed!" );
        }
        return useCredentials ? dataSource.getConnection( user, pass ) : dataSource.getConnection();
    }

    @Override
    public void closeConnection(Connection connection) throws SQLException {
        connection.close();
    }

DriverManagerConnectionProviderImpl 方法如下

@Override
public Connection getConnection() throws SQLException {
    if ( !active ) {
        throw new HibernateException( "Connection pool is no longer active" );
    }

    return pool.poll();
}

public Connection poll() throws SQLException {
        Connection conn = availableConnections.poll();
        if ( conn == null ) {
            synchronized (allConnections) {
                if(allConnections.size() < maxSize) {
                    addConnections( 1 );
                    return poll();
                }
            }
            throw new HibernateException( "The internal connection pool has reached its maximum size and no connection is currently available!" );
        }
        conn.setAutoCommit( autoCommit );
        return conn;
    }


@Override
public void closeConnection(Connection conn) throws SQLException {
    if (conn == null) {
        return;
    }

    pool.add( conn );
}

如您所见,它们都有不同的处理连接方式。如果您关闭连接,我将不会遇到重大问题,因为第一个连接将返回新的连接,第二个连接将在连接关闭时补充池。但是,如果您不打算关闭,请知道在应用程序中正在调用哪个实现,并确保您没有泄漏。

And as you can see, they both have different ways of handling connections. I would not imagine major issues if you close the connection, since the first one returns new connection and the second one replenishes the pool if the connection is closed. However, if you are not going to close, know which implementation is getting invoked in your application and make sure you do not have a leak.

其他两个实现是针对Hikari和UserSupplied模式。

The other two implementations are for Hikari and UserSupplied mode.

这篇关于如果仍然可以将Hibernate与数据库一起使用(和c3p0 dbpooling),并且可以安全地绕过Hibernate直接获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆