001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: JdbcSessionFactory.java 9071 2007-09-14 08:41:30Z lzheng $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.jdbc;
026:
027: import java.sql.Connection;
028: import java.sql.Driver;
029: import java.util.Properties;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import com.bostechcorp.cbesb.common.util.ErrorUtil;
035: import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;
036:
037: public class JdbcSessionFactory {
038:
039: private Log log = LogFactory.getLog(JdbcSession.class);
040: private Properties connectionProperties = new Properties();
041: protected Object driverInstance = null;
042: protected String driverName;
043: protected String url;
044: protected String user;
045: protected String password;
046: protected boolean autoCommit;
047: protected int connectionRetries;
048: protected int connectionInterval;
049: protected String saName;
050:
051: public JdbcSessionFactory() {
052:
053: }
054:
055: public String getSaName() {
056: return saName;
057: }
058:
059: public void setSaName(String saName) {
060: this .saName = saName;
061: }
062:
063: /**
064: * @return the driverName
065: */
066: public String getDriverName() {
067: return driverName;
068: }
069:
070: /**
071: * @param driverName the driverName to set
072: */
073: public void setDriverName(String driverName) {
074: this .driverName = driverName;
075: }
076:
077: /**
078: * @return the password
079: */
080: public String getPassword() {
081: return password;
082: }
083:
084: /**
085: * @param password the password to set
086: */
087: public void setPassword(String password) {
088: this .password = password;
089: }
090:
091: /**
092: * @return the autoCommit
093: */
094: public boolean isAutoCommit() {
095: return autoCommit;
096: }
097:
098: /**
099: * @param autoCommit the autoCommit to set
100: */
101: public void setAutoCommit(boolean autoCommit) {
102: this .autoCommit = autoCommit;
103: }
104:
105: /**
106: * @return the url
107: */
108: public String getUrl() {
109: return url;
110: }
111:
112: /**
113: * @param url the url to set
114: */
115: public void setUrl(String url) {
116: this .url = url;
117: }
118:
119: /**
120: * @return the user
121: */
122: public String getUser() {
123: return user;
124: }
125:
126: /**
127: * @param user the user to set
128: */
129: public void setUser(String user) {
130: this .user = user;
131: }
132:
133: /**
134: * @return the connectionInterval
135: */
136: public int getConnectionInterval() {
137: return connectionInterval;
138: }
139:
140: /**
141: * @param connectionInterval the connectionInterval to set
142: */
143: public void setConnectionInterval(int connectionInterval) {
144: this .connectionInterval = connectionInterval;
145: }
146:
147: /**
148: * @return the connectionRetries
149: */
150: public int getConnectionRetries() {
151: return connectionRetries;
152: }
153:
154: /**
155: * @param connectionRetries the connectionRetries to set
156: */
157: public void setConnectionRetries(int connectionRetries) {
158: this .connectionRetries = connectionRetries;
159: }
160:
161: /**
162: * Creates a new connection to the database
163: * @return new connection
164: */
165: private Connection getConnection() {
166: int retries = 1;
167: Connection connection = null;
168: do {
169: try {
170:
171: connection = ((Driver) this .driverInstance).connect(
172: url, connectionProperties);
173: if (!connection.isClosed()) {
174: connection.setAutoCommit(autoCommit);
175: break;
176: }
177: } catch (Exception e) {
178: ErrorUtil
179: .printError(
180: "SQLException caught while connecting to database.",
181: e);
182: }
183: try {
184: Thread.sleep(connectionInterval);
185: } catch (InterruptedException e) {
186: ErrorUtil.printError("Exception in getConnection()", e);
187: }
188: } while (retries++ < connectionRetries);
189:
190: return connection;
191: }
192:
193: public JdbcSession getNewSession() {
194: JdbcSession session = null;
195: connectionProperties.clear();
196:
197: if (driverInstance == null) {
198: try {
199:
200: Class driverClass = Class
201: .forName(driverName, true, RuntimeClassLoader
202: .getClassLoader(saName, this ));
203: driverInstance = driverClass.newInstance();
204: connectionProperties.setProperty("driverName",
205: driverInstance.getClass().getName());
206: } catch (ClassNotFoundException e) {
207: ErrorUtil
208: .printError(
209: "ClassNotFoundException - Specified JDBC Driver class not found.",
210: e);
211: } catch (InstantiationException e) {
212: ErrorUtil
213: .printError(
214: "InstantiationException caught while loading JDBC driver",
215: e);
216: } catch (IllegalAccessException e) {
217: ErrorUtil
218: .printError(
219: "IllegalAccessException caught while loading JDBC driver",
220: e);
221: }
222: }
223:
224: if (user != null) {
225: connectionProperties.setProperty("user", user);
226: }
227:
228: if (password != null) {
229: connectionProperties.setProperty("password", password);
230: }
231:
232: Connection conn = getConnection();
233:
234: session = new JdbcSession(conn);
235: session.setSessionId(generateSessionId());
236: session.setAutoCommit(this .autoCommit);
237:
238: return session;
239: }
240:
241: protected long generateSessionId() {
242: long threadID = Thread.currentThread().getId();
243: int len = (int) ((threadID / 10) + 1);
244: long currentTime = System.currentTimeMillis();
245: long sessionID = currentTime * (long) (Math.pow(10, len))
246: + threadID;
247: return sessionID;
248: }
249: }
|