KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > db > AddressBookDBServices


1 package org.jahia.tools.db;
2
3
4 /*
5  * Web Application: AddressBook
6  * Copyright (c) 2003 jahia.org All rights reserved.
7  *
8  */

9
10
11 import java.util.*;
12 import java.sql.*;
13
14
15 /**
16  * Class AddressBookDBServices
17  * Handle a pool of connections to the database.
18  *
19  * @author Tamiotti Jerome <a HREF="mailto:tamiotti@xo3.com">tamiotti@xo3.com</a>
20  * @version 1.0
21  */

22 public class AddressBookDBServices
23 {
24     private static AddressBookDBServices m_Instance = null;
25     private ConnectionPool m_ConnPool = null;
26
27     //== JDBC members =====
28
private static String JavaDoc m_DB_DRIVER;
29     private static String JavaDoc m_DB_URL;
30     private static String JavaDoc m_DB_USERNAME;
31     private static String JavaDoc m_DB_PASSWORD;
32     private static int m_DB_INITIALCONNECTIONS;
33     private static int m_DB_MAXCONNECTIONS;
34
35
36     /**
37      * Constructor
38      */

39     protected AddressBookDBServices()
40     {
41     try {
42           m_ConnPool = new ConnectionPool( m_DB_DRIVER, m_DB_URL, m_DB_USERNAME, m_DB_PASSWORD, m_DB_INITIALCONNECTIONS, m_DB_MAXCONNECTIONS, true );
43       } catch (SQLException se) {
44          System.out.println("Error in creation connectionPool : " + se.getMessage() + "\n" );
45       }
46      }
47
48     /**
49      * Method getInstance
50      * Should be called to instanciate this class with database parameters
51      * like database driver,url, name and password
52       *
53      * @param db_Driver the database driver
54      * @param db_Url the dtatabase url
55      * @param db_Username the database login name
56      * @param db_InitialConnections the database initial number of connections to open
57      * @param db_MaxConnections the max. number of connections to open
58       * @return a reference to the unique instance of this class
59      */

60     public static AddressBookDBServices getInstance(String JavaDoc db_Driver, String JavaDoc db_URL, String JavaDoc db_Username, String JavaDoc db_Password, int db_InitialConnections, int db_MaxConnections)
61     {
62     if ( m_Instance == null )
63     {
64         m_DB_DRIVER = db_Driver;
65         m_DB_URL = db_URL;
66         m_DB_USERNAME = db_Username;
67         m_DB_PASSWORD = db_Password;
68         m_DB_INITIALCONNECTIONS = db_InitialConnections;
69         m_DB_MAXCONNECTIONS = db_MaxConnections;
70
71         m_Instance = new AddressBookDBServices();
72     }
73     return m_Instance;
74     }
75
76
77
78     /**
79      * Method getInstance
80      * Should be called to get a reference to the unique instance of this class
81      * without the need to give database informations
82      *
83      * @return a reference to the unique instance of this class
84      * @exception SQLException
85      */

86     public static AddressBookDBServices getInstance() throws SQLException
87     {
88         if ( m_Instance == null )
89         {
90         throw new SQLException();
91         }
92     else
93     {
94         return m_Instance;
95     }
96     }
97
98     /**
99      * Method getConnection
100      * Return a database connection from the connection pool
101      *
102      * @return Connection a database connection
103      * @exception SQLException
104      */

105     public Connection getConnection()
106     throws SQLException
107     {
108     return m_ConnPool.getConnection();
109     }
110
111
112
113     /***
114      * Method freeConnection
115      * Should be called to free a database connection
116      *
117      * @param Conection the database connection to free
118      */

119      public void freeConnection( Connection connexion )
120      {
121         m_ConnPool.free( connexion );
122      }
123
124
125 }
126
Popular Tags