I will be using only single database for my project. It is pretty fixed that only one datasource (Oracle) will be used. So I create one Datasource class to create connections. I have kept the methods as static as I will be using only one datasource.
public class DataSource {
static Logger log = Logger.getLogger(DataSource.class);
static String connectionURL;
static String userName;
static String password;
static {
log.debug("Creating ConnectionURL");
Connection conn;
Properties properties = new Properties();
try {
FileInputStream in = new FileInputStream(getCodesignRoot() + "/DBConnection/config.properties");
properties.load(in);
} catch (FileNotFoundException ex) {
log.error("config.properties file not found", ex);
log.info("SHUTTING DOWN APPLICATION");
System.exit(-1);
} catch (IOException ex) {
log.error("Can't load the config.properties file", ex);
log.info("SHUTTING DOWN APPLICATION");
System.exit(-1);
}
String databaseServer = properties.getProperty("jdbc.databaseServer");
String listenerPort = properties.getProperty("jdbc.listenerPort");
String oracleSID = properties.getProperty("jdbc.oracleSID");
userName = properties.getProperty("jdbc.userName");
password = properties.getProperty("jdbc.userPassword");
connectionURL
= "jdbc:oracle:thin:@//" + databaseServer + ":" + listenerPort
+ "/" + oracleSID;
}
Most importantly, I am having get connection method as static.
public static Connection getConnection() throws SQLException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException ex) {
log.error("Driver Not Found", ex);
throw new SQLException("Driver Not Found");
}
return DriverManager.getConnection(connectionURL, userName, password);
}
Is this a good design? Where should I keep my getConnection method? Is it ok for it to be static as I will be using only one datasource?
I forgot to add one part. What If I have to use two databases. One for developemnt and one for production, both oracle databases. How should I go about that? I want to make minimal changes, when I switch from development to production.