• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

JDBC Connection Pool

Hello,
Anyone who has a code to establish a connection pool to oracle, please give me.
Thank you very much
0
nphnhi
Asked:
nphnhi
  • 3
2 Solutions
 
Jim CakalicSenior Developer/ArchitectCommented:
Here's an example of programmatic setup of BasicDataSource for Oracle:

    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    bds.setMinIdle(1);
    bds.setMaxActive(5);
    bds.setMaxIdle(5);
    bds.setMaxWait(60000);
    bds.setPassword("password");
    bds.setUrl("jdbc:oracle:thin:@server:port");
    bds.setUsername("userid");
    bds.setValidationQuery("select 0 from dual");
    bds.setTestOnBorrow(true);
    bds.setDefaultAutoCommit(true);
    bds.setDefaultReadOnly(false);

Replace URL, username, and password with appropriate values for your environment. Make sure that the classes12.jar is in the classpath.

Then, to get/use a JDBC Connection from the pool:

    Connection conn = null;
    try {
        conn = bds.getConnection();
        ...
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }

Regards,
Jim Cakalic
0
 
Jim CakalicSenior Developer/ArchitectCommented:
BTW, forgot to ask, are you developing a stand-alone Java application or are you using an application server (like JBoss, Tomcat, WebSphere, etc.) If you are using an application server then it is usually better to configure a server-managed DataSource than to create your own like this.

Jim
0
 
Jim CakalicSenior Developer/ArchitectCommented:
OK
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now