Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

java - Oracle Connection Pool Class

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?

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

Connection conn = DbConnection.getConnection();
...
conn.close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...