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 9029 2007-09-12 09:39:18Z lzheng $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.jdbc;
026:
027: import java.sql.Connection;
028:
029: import javax.naming.InitialContext;
030: import javax.sql.DataSource;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import com.bostechcorp.cbesb.common.util.ErrorUtil;
036:
037: public class JdbcJndiSessionFactory extends JdbcSessionFactory {
038:
039: private Log log = LogFactory.getLog(JdbcSession.class);
040:
041: private String dataSourceName;
042:
043: public JdbcJndiSessionFactory() {
044:
045: }
046:
047: public String getDataSourceName() {
048: return dataSourceName;
049: }
050:
051: public void setDataSourceName(String dataSourceName) {
052: this .dataSourceName = dataSourceName;
053: }
054:
055: /**
056: * Creates a new connection to the database
057: * @return new connection
058: */
059: private Connection getConnection() {
060: int retries = 1;
061: Connection connection = null;
062: do {
063: try {
064:
065: InitialContext ctx = new InitialContext();
066: DataSource dataSource = (DataSource) ctx
067: .lookup(this .dataSourceName);
068: connection = dataSource.getConnection();
069:
070: if (!connection.isClosed()) {
071: connection.setAutoCommit(autoCommit);
072: break;
073: }
074: } catch (Exception e) {
075: ErrorUtil
076: .printError(
077: "SQLException caught while connecting to database.",
078: e);
079: }
080: try {
081: Thread.sleep(connectionInterval);
082: } catch (InterruptedException e) {
083: ErrorUtil.printError("Exception in getConnection()", e);
084: }
085: } while (retries++ < connectionRetries);
086:
087: return connection;
088: }
089:
090: public JdbcSession getNewSession() {
091: JdbcSession session = null;
092:
093: Connection conn = getConnection();
094:
095: session = new JdbcSession(conn);
096: session.setSessionId(generateSessionId());
097: session.setAutoCommit(this.autoCommit);
098:
099: return session;
100: }
101:
102: }
|