简体   繁体   中英

Database Connectivity in java

First i show you the code then asked few questions. i have a class database connectivity like this (please ignore syntax error if any)

class DatabaseConnection {
private static Connection connection = null;
private static String driverName="";
private static String userName="";
private static String passwrod="";
private static String url="";

private DatabaseConnection() { }

public static void createConnection() {

    if ( connection  == null ) {
    // read database credentials from xml file and set values of driverName, userName, passowrd and url

    //create connection with database and set store this connection in connection object created a class level.
    }
}
public static void closeConnection1() throws Exception{
    if ( connection != null ) {
        connection.close();
        connection == null
    }

}

public static void closeConnection2() throws Exception{
    if ( connection != null ) {
        connection.close();
    }
}
public void insertData(Object data) {
    // insetData in database
}
}

I want to know which close connection is more optimize in database connection. Lets suppose I have test class like this

class Test {
public static void main(String args[]) {
    DatabaseConnection.createConnection();
    DatabaseConnection.insertData(data);
    DatabaseConnection.closeConnection2(); // we call also called close connection method within the insertData method after inserting the data

}
}

After creating database connection i insert data in database and then close the connection using closeConnection2 method. in this way connection has been close so if i want to insert some more method then i have to recreate connection with the database but i can't do this because connection object is not null and createConnection didn't execute the code inside the if statement. Now if I called closeConnection1 method for closing connection then in doing this i have to parse xml file again for credential which is not a optimize solution. can you tell me which method is good and if both are worse then please tell me more efficient way for creating and closing database connection.

I see two major problems with this:

  • The fact that everything (including the Connection object) is static means that you can't ever use this class from more than one thread at once.
  • parsing the configuration data and opening the connection are separate concerns and should not be mixed. At least move them into separate methods, the configuration could probably even go in another class.

The second thing alone will avoid having to parse the connection information multiple times.

An even better approach would be to use a DataSource instead of opening the connections each time. And then use a DataSource that's actually a connection pool!

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