0

I have a class called SQLConnection responsible for open and close jdbc connections. Ever time I need open a connection, I need read a xml file to charge the class atributes, and then I open the connection.

I thinking in create a singleton design pattern to this class, and read the xml file on contructor's class.

I ask: Which is the better approach and what provide a best performance? Have a static intance to the class, or read the xml and create the object class every time? Ahh, It is a web application with 10 ~~ 100 users.

Here is like I doing:

public class SQLConnection {

    String url = ""; 
    String driver = "";
    String userName = "";
    String password = "";

    public Connection openConnection() {

        Connection conn = null;     
        this.setAtributes();        

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        } 

        return conn;

    }


    public void closeConnection(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //read a xml file that have database infos
    public void setAtributes(){

        this.url = "lalala";
        this.userName = "lalala";
        this.password = "lalala";
        this.driver = "lalala";                 
    }
}

Here is like I want to do:

public class SQLConnection {

    private static SQLConnection instance;

    String url = ""; 
    String driver = "";
    String userName = "";
    String password = "";

    private SQLConnection() {

    }

    public static synchronized SQLConnection getInstance() {
        if (instance == null)
            instance = new SQLConnection();
            setAtributes(instance);

        return instance;
    }

    public Connection openConnection() {

        Connection conn = null;     

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        } 

        return conn;

    }

    //read a xml file that have database infos
    public static void setAtributes(SQLConnection instance){

        instance.url = "lalala";
        instance.userName = "lalala";
        instance.password = "lalala";
        instance.driver = "lalala";     
    }
}
mbomb007
  • 3,077
  • 2
  • 30
  • 51

1 Answers1

0

Short answer: No.

Singletons solve Resource Contention problem. For SQL connection there is no contention, you can create as many connections as you want. More details about using singletons are here: What is so bad about singletons?

As other people mentioned it's better to use a connection pool. You will get better performance with connection pool as well. On benefits of connection pools: Why we need a connection pooling for JDBC?

Community
  • 1
  • 1
DmitriD
  • 106
  • 1
  • 3