001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.databaseSQL;
025:
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032: import java.util.Properties;
033:
034: import org.objectweb.salome_tmf.api.Util;
035: import org.objectweb.salome_tmf.api.sql.IDataBase;
036:
037: public class DataBase implements IDataBase {
038: /**
039: * Connexion e la BdD
040: */
041: Connection cnt = null;
042: //private String driver = "com.mysql.jdbc.Driver";
043: private String driver = "org.gjt.mm.mysql.Driver";
044: //Savepoint pSavepoint;
045:
046: private String poolUrl;
047: private Properties poolInfo;
048: private static String proxoolMaximumActiveTime = "14400000";
049:
050: public DataBase(String driverJDBC) throws Exception {
051: driver = driverJDBC;
052: Class.forName(driver).newInstance();
053: Util.log("[DataBase] connection to database with jdbc "
054: + driver + " is OK");
055: }
056:
057: private void configurePoolConnection(String url, String username,
058: String password) throws Exception {
059: String poolAlias = "SalomePool";
060:
061: poolInfo = new Properties();
062: poolInfo.setProperty("proxool.maximum-connection-count", "20");
063: poolInfo.setProperty("proxool.house-keeping-test-sql",
064: "select CURRENT_DATE");
065: poolInfo.setProperty("proxool.trace", "true");
066: poolInfo.setProperty("proxool.maximum-active-time",
067: proxoolMaximumActiveTime);
068: Util.log("[DataBase] pool connection maximum active time is "
069: + proxoolMaximumActiveTime + " ms");
070: poolInfo.setProperty("user", username);
071: poolInfo.setProperty("password", password);
072: String alias = poolAlias;
073: String driverClass = driver;
074: String driverUrl = url;
075: poolUrl = "proxool." + alias + ":" + driverClass + ":"
076: + driverUrl;
077:
078: Class.forName("org.logicalcobwebs.proxool.ProxoolDriver")
079: .newInstance();
080: }
081:
082: public void openPoolConnection(String url, String username,
083: String password) throws Exception {
084: try {
085: Util.log("DB " + url + ", user : " + username
086: + ", paswd : " + password);
087:
088: // Pool de connexion par DBCP
089: // BasicDataSource ds = new BasicDataSource();
090: // ds.setDriverClassName(driver);
091: // ds.setUsername(username);
092: // ds.setPassword(password);
093: // ds.setUrl(url);
094: // cnt = ds.getConnection();
095:
096: // Pool de connexion par Proxool
097: if (poolUrl == null || poolInfo == null) {
098: configurePoolConnection(url, username, password);
099: }
100: cnt = DriverManager.getConnection(poolUrl, poolInfo);
101:
102: cnt.setAutoCommit(false);
103:
104: Util.log("[DataBase->open] Pool Connection (proxool) : "
105: + cnt);
106:
107: } catch (SQLException ex) {
108: Util.log("Echec d'ouverture :" + ex.getMessage());
109: throw ex;
110: }
111: }
112:
113: /**
114: * Fonction qui ouvre la connexion e la base de donnees
115: *
116: * @param url
117: * @param username
118: * @param password
119: */
120: public void open(String url, String username, String password)
121: throws Exception {
122: try {
123: Util.log("DB " + url + ", user : " + username
124: + ", paswd : " + password);
125: cnt = DriverManager.getConnection(url, username, password);
126: //dmd = cnt.getMetaData();
127: //results = new DataSet(dmd.getCatalogs());
128: cnt.setAutoCommit(false);
129:
130: Util.log("[DataBase->open] Connection : " + cnt);
131:
132: } catch (SQLException ex) {
133: Util.log("Echec d'ouverture :" + ex.getMessage());
134: throw ex;
135: }
136: }
137:
138: /**
139: * Fonction qui ouvre la connexion e la base de donnees
140: *
141: * @param url
142: * @param username
143: * @param password
144: */
145: public void open(String url, String username, String password,
146: java.util.Properties prop_proxy) throws Exception {
147: try {
148: Util.log("DB " + url + ", user : " + username + "paswd "
149: + password);
150: if (prop_proxy != null) {
151: prop_proxy.put("user", username);
152: prop_proxy.put("password", password);
153: Util.log("Connect to DB with properties");
154: cnt = DriverManager.getConnection(url, prop_proxy);
155: } else
156: cnt = DriverManager.getConnection(url, username,
157: password);
158: //dmd = cnt.getMetaData();
159: //results = new DataSet(dmd.getCatalogs());
160: cnt.setAutoCommit(false);
161: } catch (SQLException ex) {
162: Util.log("Echec d'ouverture :" + ex.getMessage());
163: throw ex;
164: }
165: }
166:
167: void loadJDBCDriver(String driver) throws Exception {
168: Class.forName(driver).newInstance();
169: }
170:
171: /*
172: * Run SQL Query in DataBase
173: */
174: public ResultSet executeQuery(String sql) throws Exception {
175: Statement stmt = cnt.createStatement();
176: return stmt.executeQuery(sql);
177: }
178:
179: /*
180: * Run SQL Update in DataBase
181: */
182: public void executeUpdate(String sql) throws Exception {
183: Statement stmt = cnt.createStatement();
184: stmt.executeUpdate(sql);
185: }
186:
187: /*
188: * Compile SQL
189: *
190: */
191: public PreparedStatement prepareStatement(String sql)
192: throws Exception {
193: PreparedStatement prep = null;
194: prep = cnt.prepareStatement(sql);
195: return prep;
196: }
197:
198: public void beginTrans() throws SQLException {
199: //pSavepoint = cnt.setSavepoint();
200: PreparedStatement prep = null;
201: //prep = cnt.prepareStatement("BEGIN TRANSACTION");
202: prep = cnt.prepareStatement("BEGIN");
203: prep.execute();
204: //cnt.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
205: }
206:
207: public void commit() throws SQLException {
208: //cnt.commit();
209: PreparedStatement prep = null;
210: prep = cnt.prepareStatement("COMMIT");
211: prep.execute();
212: //pSavepoint = null;
213: }
214:
215: public void rollback() throws SQLException {
216: //cnt.rollback();
217: PreparedStatement prep = null;
218: prep = cnt.prepareStatement("ROLLBACK");
219: prep.execute();
220: //cnt.rollback(pSavepoint);
221: }
222:
223: public void close() throws Exception {
224: cnt.close();
225: }
226:
227: public Connection getCnt() throws Exception {
228: return cnt;
229: }
230:
231: public void setCnt(Connection cnt) throws Exception {
232: this .cnt = cnt;
233: }
234:
235: public static void setProxoolMaximumActiveTime(
236: String proxoolMaximumActiveTime) throws Exception {
237: DataBase.proxoolMaximumActiveTime = proxoolMaximumActiveTime;
238: }
239: }
|