Oracle连接池类 [英] Oracle Connection Pool Class

查看:93
本文介绍了Oracle连接池类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Helper类中为Oracle DB设置连接池.

I want to setup a connection pool for a Oracle DB in a Helper class.

public class DbConnection {

// Data source for the pooled connection
private static OracleDataSource dataSource;

// Host
private static final String dbHost = "bla";

// Port
private static final String dbPort = "1521";

// DBname
private static final String database = "orcl";

// DBuser
private static final String dbUser = "bla";

// DBpassword
private static final String dbPassword = "bla";

static {
    OracleConnectionPoolDataSource opds;
    try {
        opds = new OracleConnectionPoolDataSource();
        opds.setURL("jdbc:oracle:thin:@" + dbHost + ":" + dbPort + ":"
                + database);
        opds.setUser(dbUser);
        opds.setPassword(dbPassword);
        dataSource = opds;
    } catch (SQLException e1) {
        System.err.println("Connection failed!");
    }
    try {
        // Load driver
        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {
        System.out.println("Driver not found!");
    }
}

public static Connection getConnection() throws SQLException {
    return dataSource.getConnection();
}
}

这正在工作,但是速度不是很快,所以我认为我缺少使池工作的东西.有什么建议吗?

This is working but it is not awfully fast so I think I'm missing something to get the pooling working. Any suggestions?

所以我的外部类只调用了getConnection()方法...

So my externel classes just invoke the getConnection() method ...

Connection conn = DbConnection.getConnection();
...
conn.close();

推荐答案

您不应直接使用ConnectionPoolDataSource.它供应用程序服务器中的连接池使用.它本身不提供连接池.另请参见 https://stackoverflow.com/a/12651163/466862

You should not use a ConnectionPoolDataSource directly. It is intended for use by a connection pool in an application server. It does not provide connection pooling itself. See also https://stackoverflow.com/a/12651163/466862

换句话说:您需要使用一个实际的连接池,例如DBCP,c3p0或BoneCP或UCP(通用连接池).

In other words: You need to use an actual connection pool, like DBCP, c3p0 or BoneCP, or the UCP (Universal Connection Pool).

这篇关于Oracle连接池类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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