简体   繁体   中英

Sharing database connection in Java EE

I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10).

I have developed java class which returns a static connection , and that connection will be used by my entire web application.

public class DBConnection 
{
    private static Connection con = null;

    static Connection getConnection(String str)
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("MyValuesHere");
            System.out.println("New instance of Connection is created for: "+str);
        }
        catch(ClassNotFoundException cnfe)
        {
            System.out.println("Error loading class!");
            cnfe.printStackTrace();
        }
        catch(SQLException sqle)
        {
            System.out.println("Error connecting to the database!");
            sqle.printStackTrace();
        }
        return con;
    }//getConnection()
}//class

Above class is working fine. Then I have another 4 java classes for

  1. Inserting
  2. Updating
  3. Deleting
  4. Selecting

data from database using the above connection . So in those 4 classes I am getting connection which is created in my DBConnection class, and those four classes are also working fine. This four classes are used in my all Servlet's.

To get Connection in those 4 classes I am writing following line:

private static Connection con = DBConnection.getConnection("UpdateQuery.java");

But problem is that I want to share the same connection in all four classes, but connection is created separately in those 4 classes. So how should I share the same connection in those four classes? is there better way of doing this? and if I use this approach will there be any issues in web application because of sharing the connection for whole application?

You are (implicitly) trying to solve a non-trivial task.

Such things are normally done by the container - taking connections from a pool, then returning them back, reconnection etc...

If you use a fully functional applications server you'd better configure and use data sources.

If your server doesn't support data sources, do not mess up with saving connection into a private field. What for example happenes when your connection is lost? Your private variable will have a non-working connection. Do you have any recovery mechanism?

Your code will be much more robust if you get it in the beginning of the business operation and then close it.

Or try to find a professionally written library that supports connection pools - it will do pretty much the same as a classic container in handling a connection pool.

Or write it yourself, but it will be a separate task with many questions.

Looks like you wanted to turn Connection into a singleton but then forgot to check whether it's been instantiated already. In getConnection you could check if con is not null in the first place and return that instance right away. Only if con is still null, proceed with initialization.

You should save the created connection instance into a private static field in DBConnection , and when getConnection is called, you check if the field is null, then create the connection, then return it: if (connection == null) { connection = createConnection(); } return connection; if (connection == null) { connection = createConnection(); } return connection;

where connection is a private static Connection connection field of DBConnection class.

However I strongly suggest to not use this approach as sharing a connection between concurrent threads will cause serious problems. I suggest to use connection pooling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM