1 /*** 2 * The contents of this file are subject to the Mozilla Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/MPL/ 6 * 7 * Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 * implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is pow2toolkit library. 13 * 14 * The Initial Owner of the Original Code is 15 * Power Of Two S.R.L. (www.pow2.com) 16 * 17 * Portions created by Power Of Two S.R.L. are 18 * Copyright (C) Power Of Two S.R.L. 19 * All Rights Reserved. 20 * 21 * Contributor(s): 22 */ 23 24 package com.pow2.dao; 25 26 27 import java.sql.Connection; 28 import java.sql.SQLException; 29 30 import org.apache.log4j.Category; 31 32 33 /*** 34 * ConnectionFactory class. 35 * <br> 36 * Provides SQL Connection objects using the 37 * underlying ConnectionProvider instance. 38 * 39 * @author Luca Fossato <fossato@pow2.com> 40 * @created 25 giugno 2002 41 */ 42 public class ConnectionFactory 43 { 44 /*** Log4j category */ 45 private Category cat = Category.getInstance(this.getClass()); 46 47 /*** an handle to the unique ConnectionFactory instance. */ 48 private static ConnectionFactory instance = null; 49 50 /*** default ConnectionProvider instance */ 51 private ConnectionProvider provider = null; 52 53 54 /*** 55 * Constructor for the ConnectionFactory object 56 */ 57 private ConnectionFactory() 58 { 59 } 60 61 62 /*** 63 * Get the unique instance of ConnectionFactory class. 64 * 65 * @return the instance of ConnectionFactory class 66 */ 67 public static synchronized ConnectionFactory instance() 68 { 69 if (instance == null) 70 instance = new ConnectionFactory(); 71 72 return instance; 73 } 74 75 76 /*** 77 * Set the ConnectionProvider object 78 * 79 * @param prefs the connection provider preferences object 80 * @throws Exception if any error occurs 81 */ 82 public synchronized void setProvider(ConnectionProviderPrefs prefs) 83 throws Exception 84 { 85 String providerClass = prefs.getConnectionProviderClass(); 86 provider = (ConnectionProvider)Class.forName(providerClass).newInstance(); 87 provider.setPrefs(prefs); 88 provider.init(); 89 90 cat.info("::setProvider - ConnectionProvider [" + providerClass + "] successfully set and initialized"); 91 } 92 93 94 /*** 95 * Get a connection object from the underlying ConnectionProvider object. 96 * 97 * @return the connection object from the underlying ConnectionProvider object 98 * @throws SQLException if any error occurs 99 */ 100 public Connection getConnection() throws SQLException 101 { 102 return provider.getConnection(); 103 } 104 105 106 /*** 107 * Get a "transactional" JDBC connection from the underlying default ConnectionProvider. 108 * 109 * @param isolationLevel the isolation level to set the connection to 110 * @return the new "transactional" connection object 111 * from the underlying default ConnectionProvider 112 * @throws SQLException if any error occurs 113 */ 114 public Connection getConnection(int isolationLevel) throws SQLException 115 { 116 return provider.getConnection(isolationLevel); 117 } 118 }