package util;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.tomcat.dbcp.dbcp2.*;


public class DBUtil {
	private static Connection conn;

	public static Connection getConnection() {
		Logger logger = Logger.getLogger(DBUtil.class.getName());
		logger.log(Level.INFO,"No open connection");
		
	
			Context initContext;
			try {
				initContext = new InitialContext();
				Context envContext = (Context) initContext.lookup("java:comp/env");
				BasicDataSource ds = (BasicDataSource) envContext.lookup("jdbc/UsersDB");
				try {
					conn = ds.getConnection();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
		
			
		return conn;
		

	}

	public static void closeConnection(Connection toBeClosed) {
		if (toBeClosed == null)
			return;

		try {
			toBeClosed.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	}
}

@@ -273,16 +273,19 @@
 Util %7B%0D%0A
+%09%0D%0A
 %09private
@@ -289,80 +289,107 @@
 ate 
-static Connection conn
+BasicDataSource ds
 ;%0D%0A
+%09
 %0D%0A%09
-public static Connection getConnection
+%0D%0A%09 private static DBUtil instance = null;%0D%0A%09   private DBUtil
 () %7B%0D%0A
+%09%09%09%0D%0A%09
 %09%09Lo
@@ -447,16 +447,17 @@
 ());%0D%0A%09%09
+%09
 logger.l
@@ -500,16 +500,18 @@
 %0D%0A%09%09
-%0D%0A%09
+   
 %0D%0A%09%09
-%09
+   
 Cont
@@ -528,16 +528,19 @@
 ntext;%0D%0A
+%0D%0A%09
 %09%09%09try %7B
@@ -537,24 +537,25 @@
 %0A%09%09%09%09try %7B%0D%0A
+%09
 %09%09%09%09initCont
@@ -579,24 +579,25 @@
 Context();%0D%0A
+%09
 %09%09%09%09Context 
@@ -661,32 +661,17 @@
 );%0D%0A%09%09%09%09
-BasicDataSource 
+%09
 ds = (Ba
@@ -725,52 +725,8 @@
 );%0D%0A
-%09%09%09%09try %7B%0D%0A%09%09%09%09%09conn = ds.getConnection();%0D%0A
 %09%09%09%09
@@ -726,35 +726,38 @@
 ;%0D%0A%09%09%09%09%7D catch (
-SQL
+Naming
 Exception e) %7B%0D%0A
@@ -833,32 +833,320 @@
 %09%09%09%7D%0D%0A%09%09
-%09%7D catch (Naming
+%0D%0A%09%09%09%0D%0A%09   %7D%0D%0A%0D%0A%09   public static DBUtil getInstance() %7B%0D%0A%09      if(instance == null) %7B%0D%0A%09         instance = new DBUtil();%0D%0A%09      %7D%0D%0A%09      return instance;%0D%0A%09   %7D%0D%0A%09   %0D%0A%09   %0D%0A%0D%0A%09public static Connection getConnection() %7B%0D%0A%09%0D%0A%09%09try %7B%0D%0A%09%09%09return DBUtil.getInstance().ds.getConnection();%0D%0A%09%09%7D catch (SQL
 Exceptio
@@ -1153,17 +1153,16 @@
 n e) %7B%0D%0A
-%09
 %09%09%09// TO
@@ -1195,17 +1195,16 @@
 ock%0D%0A%09%09%09
-%09
 e.printS
@@ -1223,25 +1223,9 @@
 %0D%0A%09%09
-%09%7D %0D%0A%09%09%09%0D%0A%09%09%0D%0A%09%09%09
+%7D
 %0D%0A%09%09
@@ -1235,17 +1235,13 @@
 urn 
-conn;%0D%0A%09%09
+null;
 %0D%0A%0D%0A

changed to singleton