001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.connect.impl;
010:
011: import com.completex.objective.components.persistency.connect.ConnectionManager;
012: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
013: import com.completex.objective.components.pool.ResourceFactory;
014: import com.completex.objective.components.log.Log;
015: import com.completex.objective.components.OdalRuntimeException;
016: import com.completex.objective.util.PropertyMap;
017:
018: import java.sql.Connection;
019: import java.sql.DriverManager;
020: import java.sql.SQLException;
021: import java.util.Properties;
022:
023: /**
024: * @author Gennady Krizhevsky
025: */
026: public class ConnectionManagerImpl implements ConnectionManager,
027: ResourceFactory {
028: private Log logger = Log.NULL_LOGGER;
029: private final String driver;
030: private final String url;
031: private final String user;
032: private final String password;
033: private Class jdbcDriver = null;
034: private PropertyMap propertyMap;
035: private int maxAttempts = DEFAULT_NUMBER_OF_ATTEMPTS;
036:
037: public ConnectionManagerImpl(PropertyMap properties) {
038: driver = properties.getProperty(PROP_DRIVER, true);
039: url = properties.getProperty(PROP_URL, true);
040: user = properties.getProperty(PROP_USER, true);
041: password = properties.getProperty(PROP_PASSWORD);
042: maxAttempts = properties.getInt(PROP_MAX_ATTEMPTS,
043: DEFAULT_NUMBER_OF_ATTEMPTS);
044: this .propertyMap = properties;
045: }
046:
047: public ConnectionManagerImpl(String driver, String url,
048: String user, String password) {
049: this .driver = driver;
050: this .url = url;
051: this .user = user;
052: this .password = password;
053: }
054:
055: public Log getLogger() {
056: return logger;
057: }
058:
059: public void setLogger(Log logger) {
060: if (logger != null) {
061: this .logger = logger;
062: }
063: }
064:
065: public int getMaxAttempts() {
066: return maxAttempts;
067: }
068:
069: public void setMaxAttempts(int maxAttempts) {
070: if (maxAttempts == INFINITE_NUMBER_OF_ATTEMPTS
071: || maxAttempts > 0) {
072: this .maxAttempts = maxAttempts;
073: }
074: }
075:
076: public String getDriver() {
077: return driver;
078: }
079:
080: public String getUrl() {
081: return url;
082: }
083:
084: public String getUser() {
085: return user;
086: }
087:
088: public String getPassword() {
089: return password;
090: }
091:
092: /**
093: * @see ConnectionManager#createConnection()
094: */
095: public Connection createConnection() {
096: if (jdbcDriver == null) {
097: try {
098: jdbcDriver = Class.forName(getDriver());
099: } catch (ClassNotFoundException e) {
100: throw new RuntimeException(
101: "Cannot load JDBC Driver class: " + getDriver(),
102: e);
103: }
104: }
105: Properties info;
106: if (propertyMap == null) {
107: info = new Properties();
108: info.put("user", user);
109: info.put("password", password);
110: } else {
111: info = propertyMap;
112: }
113: return connect(info);
114: }
115:
116: private Connection connect(Properties info) {
117: int numberOfAttempts = 0;
118: Throwable cause = null;
119: while (true) {
120: if (numberOfAttempts == INFINITE_NUMBER_OF_ATTEMPTS
121: || numberOfAttempts < maxAttempts) {
122: try {
123: Connection connection = DriverManager
124: .getConnection(getUrl(), info);
125: connection.setAutoCommit(false);
126: return connection;
127: } catch (SQLException e) {
128: try {
129: Thread.sleep(1000);
130: } catch (InterruptedException e1) {
131: throw new OdalRuntimeException(
132: "ConnectionManagerImpl::connect InterruptedException");
133: }
134: if (numberOfAttempts != INFINITE_NUMBER_OF_ATTEMPTS) {
135: numberOfAttempts++;
136: getLogger().warn(
137: "ConnectionManagerImpl::connect (after SQLException) numberOfAttempts = "
138: + numberOfAttempts);
139: }
140: cause = e;
141: }
142: } else {
143: throw new OdalRuntimePersistencyException(
144: "Cannot create database connection [user = '"
145: + getUser() + "'; password = '"
146: + getPassword() + "'; url = '"
147: + getUrl() + "'] in " + maxAttempts
148: + " attempts", cause);
149: }
150: }
151: }
152:
153: /**
154: * @see ConnectionManager#destroyConnection(Connection connection)
155: */
156: public void destroyConnection(Connection connection) {
157: try {
158: connection.close();
159: } catch (SQLException e) {
160: throw new OdalRuntimePersistencyException(
161: "Cannot close database connection", e);
162: }
163: }
164:
165: /**
166: * @see ResourceFactory#createResource()
167: */
168: public Object createResource() {
169: return createConnection();
170: }
171:
172: /**
173: * @see ResourceFactory#destroyResource(Object resource)
174: */
175: public void destroyResource(Object resource) {
176: destroyConnection(((Connection) resource));
177: }
178: }
|