Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA JDBC reusing connections

Tags:

java

jdbc

I have a Java program in which I am doing some JDBC for select queries. Will it be advisable to call testDataBase() each time which inturns calls DBConnection() each time or I should reuse one connection for all the queries. Thanks in advance.

private  void testDataBase(String query){
    Connection con = DBConnection();
    Statement st = null;
    ResultSet rs = null;

    try {
        st = con.createStatement();
        rs = st.executeQuery(query);
        boolean flag = true;
        while (rs.next()) {
            String resultString = "";
            for(int i = 1; i <=rs.getMetaData().getColumnCount();i++){
                resultString=resultString+" "+  rs.getString(i);
            }
            System.out.println(resultString);
        }
    } catch (SQLException e) {
        e.printStackTrace();

    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}               



private  Connection DBConnection() {
    final String method_name =  "DBConnection";
    Connection conn = null;
    try{
      Class.forName(driver).newInstance();
      conn = java.sql.DriverManager.getConnection(url,userName,password);

    }catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return conn;
}
like image 446
user1582625 Avatar asked Mar 13 '13 10:03

user1582625


People also ask

What is reusing database object in Java?

1. Reusing Database Connections. A servlet can create one or more Connectionobjects in its init() method and reuse them in its service(), doGet(), and doPost() methods. To demonstrate, Example 9-4 shows the phone lookup servlet rewritten to create its Connection object in advance.

What is connection pooling in JDBC?

A JDBC connection pool is a group of reusable connections for a particular database. Because creating each new physical connection is time consuming, the server maintains a pool of available connections to increase performance. When an application requests a connection, it obtains one from the pool.

Why do we need connection pooling?

Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.

What is the advantage of using JDBC connection pool?

Which of the following is advantage of using JDBC connection pool? Explanation: Since the JDBC connection takes time to establish. Creating connection at the application start-up and reusing at the time of requirement, helps performance of the application.


2 Answers

Opening a DB connection is an expensive operation in terms of perfofmance. You should use a ConnectionPool for sharing connections among different requests.

like image 170
Amir Kost Avatar answered Oct 09 '22 02:10

Amir Kost


Connections are not thread safe, so sharing them across requests is not a good idea.

A better idea is to pool connections and keep their scope as narrow as possible: check the connection out of the pool, use it, close it in transaction scope.

like image 23
duffymo Avatar answered Oct 09 '22 02:10

duffymo