The Servlet which loads the Singleton class should be loaded during the server startup
ConfigServlet.java
public class ConfigServlet extends HttpServlet { @Override public void init() throws ServletException { super.init(); SingletonDBConnection.getInstance(); } }
web.xml
<servlet> <servlet-name>StartUpServlet</servlet-name> <servlet-class>com.mugil.tutor.ConfigServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
DBConnection.java
public class DBConnection { public Connection getDBConnection() { Connection connection = null; try { connection = DriverManager.getConnection( "HOST_NAME", "USER_NAME", "PASSWORD"); } catch (SQLException e) { e.getLocalizedMessage(); System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return null; } return connection; } }
SingletonDBConnection.java
import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class SingletonDBConnection { private static SingletonDBConnection singleInstance; private static DataSource dataSource; private static Connection dbConnect; private SingletonDBConnection() { try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); dataSource = (DataSource) envContext.lookup("jdbc/testdb"); try { dbConnect = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } catch (NamingException e) { e.printStackTrace(); } } public static SingletonDBConnection getInstance() { if(singleInstance == null) { synchronized (SingletonDBConnection.class) { if(singleInstance == null) { singleInstance = new SingletonDBConnection(); } } } return singleInstance; } public static Connection getConnInst() { try { dbConnect = dataSource.getConnection(); } catch (SQLException e1) { e1.printStackTrace(); } if(dbConnect == null) { try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); dataSource = (DataSource) envContext.lookup("jdbc/testdb"); try { dbConnect = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } catch (NamingException e) { e.printStackTrace(); } } return dbConnect; } }
ListUsers.java
public List<User> getUsersList() { Connection conn; Statement stmt = null; ResultSet rs; List<User> arrUsersList = new ArrayList<User>(); conn = SingletonDBConnection.getInstance().getConnInst(); String strSQL = "SELECT UserId, UserName, Gender, UserLocation " + " FROM tblusers"; try { stmt = conn.createStatement(); rs = stmt.executeQuery(strSQL); while(rs.next()) { User objUser = new User(); objUser.setUserId(rs.getString("UserId")); objUser.setUserName(rs.getString("UserName")); objUser.setUserGender(rs.getString("Gender")); objUser.setUserLocation(rs.getString("UserLocation")); arrUsersList.add(objUser); } } catch (SQLException e) { e.printStackTrace(); } finally { if(conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return arrUsersList; }