Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConnectionPoolManager {
- private static org.apache.log4j.Logger log;
- private String databaseUrl;
- String userName = null;
- String password = null;
- Properties prop = new Properties();
- Vector connectionPool = new Vector();
- public ConnectionPoolManager() {
- databaseUrl();
- initialize();
- }
- public void databaseUrl() {
- try {
- URL url = getClass().getClassLoader().getResource("database.properties");
- // load a properties file
- prop.load(new FileInputStream(url.getPath()));
- // get the property value
- databaseUrl = prop.getProperty("database");
- } catch (IOException ex) {
- log.error("Date & Time: " + new Date() + ", Class Name : ConnectionPoolManager, Method Name : databaseUrl"
- + "n" + "Error : " + ex.getMessage());
- } catch (Exception e) {
- log.error("Date & Time: " + new Date() + ", Class Name : ConnectionPoolManager, Method Name : databaseUrl"
- + "n" + "Error : " + e.getMessage());
- }
- userName = prop.getProperty("username");
- password = prop.getProperty("password");
- }
- public ConnectionPoolManager(String databaseUrl, String userName, String password) {
- this.databaseUrl = databaseUrl;
- this.userName = userName;
- this.password = password;
- initialize();
- }
- private void initialize() {
- // Here we can initialize all the information that we need
- initializeConnectionPool();
- }
- private void initializeConnectionPool() {
- while (!checkIfConnectionPoolIsFull()) {
- System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
- // Adding new connection instance until the pool is full
- connectionPool.addElement(createNewConnectionForPool());
- }
- System.out.println("Connection Pool is full");
- }
- private synchronized boolean checkIfConnectionPoolIsFull() {
- final int MAX_POOL_SIZE = 5;
- // Check if the pool size
- if (connectionPool.size() < 5) {
- return false;
- }
- return true;
- }
- // Creating a connection
- private Connection createNewConnectionForPool() {
- Connection connection = null;
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- // connection = DriverManager.getConnection(databaseUrl, userName,
- // password);
- connection = DriverManager.getConnection(databaseUrl);
- } catch (SQLException sqle) {
- System.err.println("SQLException: " + sqle);
- return null;
- } catch (ClassNotFoundException cnfe) {
- System.err.println("ClassNotFoundException: " + cnfe);
- return null;
- }
- return connection;
- }
- public synchronized Connection getConnectionFromPool() {
- Connection connection = null;
- // Check if there is a connection available. There are times when all
- // the connections in the pool may be used up
- if (connectionPool.size() > 0) {
- connection = (Connection) connectionPool.firstElement();
- connectionPool.removeElementAt(0);
- }
- // Giving away the connection from the connection pool
- return connection;
- }
- /**
- * The method adds the connection from the client back to the connection
- * pool
- *
- * @return void
- * @param connection
- */
- public synchronized void returnConnectionToPool(Connection connection) {
- // Adding the connection from the client back to the connection pool
- connectionPool.addElement(connection);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement