001: package net.xoetrope.optional.data.sql;
002:
003: import java.sql.Connection;
004: import java.sql.DriverManager;
005: import java.sql.SQLException;
006:
007: import net.xoetrope.optional.pool.PoolManager;
008: import net.xoetrope.optional.pool.PoolObject;
009:
010: /**
011: * A class that acts as a Connection Pool manager.
012: * <p>Copyright (c) Xoetrope Ltd. 2001-2003</p>
013: * $Revision: 1.2 $
014: */
015: public class ConnectionManager extends PoolManager {
016: protected String url, user, password;
017: protected static String defaultDriverName = "sun.jdbc.odbc.JdbcOdbcDriver";
018: protected static String defaultDatabaseUrl = "jdbc:odbc:xlib";
019:
020: protected static ConnectionManager connMgr = null;
021:
022: /**
023: * Constructs a new ConnectionManager, called by getInstance
024: * @param adriver the database driver name
025: * @param aurl the database connection string
026: * @param auser the database user
027: * @param apassword the database password
028: * @param size the initial pool size
029: */
030: protected ConnectionManager(String adriver, String aurl,
031: String auser, String apassword, int size) {
032: super (size);
033:
034: defaultDriverName = adriver;
035: url = aurl;
036: user = auser;
037: password = apassword;
038: }
039:
040: /**
041: * Creates a new ConnectionObject, loading the JDBC driver in the process if necessary
042: * @param connName the connection name, ignored for this the default
043: * connection manager as only a single instance of the default connection is
044: * used.
045: * @return
046: * @throws SQLException
047: */
048: public PoolObject getNewObject(String connName) throws SQLException {
049: try {
050: Class.forName(defaultDriverName);
051: } catch (ClassNotFoundException e) {
052: }
053:
054: Connection conn = DriverManager.getConnection(url, user,
055: password);
056: return new ConnectionObject(conn, this );
057: }
058:
059: /**
060: * Gets an instance of the ConnectionManager
061: * @return
062: */
063: public static ConnectionManager getInstance() {
064: if (connMgr != null)
065: return connMgr;
066:
067: return connMgr = new ConnectionManager(defaultDriverName,
068: defaultDatabaseUrl, "sa", "", 10);
069: }
070:
071: /**
072: * Gets an instance of the ConnectionManager. Any open connections are closed
073: * prior to attempting the new connection.
074: * @return
075: */
076: public ConnectionManager reset(String driver, String url,
077: String userName, String password) {
078: if (connMgr != null)
079: connMgr.closeObjects();
080:
081: defaultDriverName = driver;
082:
083: try {
084: // Try to load the driver.
085: Class.forName(defaultDriverName);
086:
087: // Attemp to make the connection
088: return connMgr = new ConnectionManager(defaultDriverName,
089: url, userName, password, 10);
090: } catch (ClassNotFoundException e) {
091: e.printStackTrace();
092: }
093:
094: return null;
095: }
096:
097: /**
098: * Gets a connection.
099: * @param connName the connection name or null if none exists
100: * @return ConnectionObject
101: * @throws SQLException
102: */
103: public ConnectionObject getConnection(String connName)
104: throws SQLException {
105: try {
106: return (ConnectionObject) ConnectionManager.getInstance()
107: .getObject(connName);
108: } catch (Exception ex) {
109: throw new SQLException(ex.getMessage());
110: }
111: }
112: }
|