1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.conprovider;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.sql.Connection;
30 import java.sql.SQLException;
31
32
33
34 /***
35 * ConnectionFactory class. <br>
36 * Provides SQL Connection objects using the underlying ConnectionProvider
37 * instance.
38 *
39 * @author Luca Fossato
40 */
41 public class ConnectionFactory {
42 /*** an handle to the unique ConnectionFactory instance. */
43 private static ConnectionFactory instance = null;
44
45 /*** default ConnectionProvider instance */
46 private AbstractConnectionProvider provider = null;
47
48 /*** Log4j category */
49 private Log cat = LogFactory.getLog(this.getClass());
50
51 /***
52 * Constructor for the ConnectionFactory object
53 */
54 private ConnectionFactory() {
55 }
56
57 /***
58 * Get the unique instance of ConnectionFactory class.
59 *
60 * @return the instance of ConnectionFactory class
61 */
62 public static synchronized ConnectionFactory instance() {
63 if (instance == null) {
64 instance = new ConnectionFactory();
65 }
66
67 return instance;
68 }
69
70
71 /***
72 * Get a connection object from the underlying ConnectionProvider object.
73 *
74 * @return the connection object from the underlying ConnectionProvider
75 * object
76 *
77 * @throws SQLException if any error occurs
78 */
79 public Connection getConnection() throws SQLException {
80 return provider.getConnection();
81 }
82
83
84 /***
85 * Get a "transactional" JDBC connection from the underlying default
86 * ConnectionProvider.
87 *
88 * @param isolationLevel the isolation level to set the connection to
89 *
90 * @return the new "transactional" connection object from the underlying
91 * default ConnectionProvider
92 *
93 * @throws SQLException if any error occurs
94 */
95 public Connection getConnection(int isolationLevel)
96 throws SQLException {
97 return provider.getConnection(isolationLevel);
98 }
99
100
101 /***
102 * Set the ConnectionProvider object
103 *
104 * @param prefs the connection provider preferences object
105 *
106 * @throws Exception if any error occurs
107 */
108 public void setProvider(ConnectionProviderPrefs prefs)
109 throws Exception {
110 String providerClass = prefs.getConnectionProviderClass();
111 provider = (AbstractConnectionProvider) Class.forName(providerClass)
112 .newInstance();
113 provider.setPrefs(prefs);
114 provider.init();
115
116 cat.info("::setProvider - ConnectionProvider [" + providerClass
117 + "] successfully set and initialized");
118 }
119 }