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";
}
}