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 java.sql.Connection;
27 import java.sql.DriverManager;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
31
32 import java.util.Date;
33 import java.util.Properties;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.dbforms.util.Util;
38
39
40
41
42 /***
43 * Single Connection provider. <br> provides one connection for all
44 *
45 * @author Henner Kollmann
46 */
47 public class SingleConnectionProvider extends AbstractConnectionProvider {
48 private static Connection con;
49 private static Date conNextValidationDate;
50 private static Log logCat = LogFactory.getLog(SinglePerThreadConnectionProvider.class);
51
52
53 /***
54 * Default constructor.
55 *
56 * @exception Exception Description of the Exception
57 * @throws Exception because of the <code>throws Exception</code> clause of
58 * the <code>init</code> method.
59 */
60 public SingleConnectionProvider() throws Exception {
61 super();
62 }
63
64 /***
65 * Get a JDBC Connection
66 *
67 * @return a JDBC Connection
68 *
69 * @exception SQLException Description of the Exception
70 */
71 protected synchronized Connection getConnection() throws SQLException {
72 long validationInterval;
73
74
75 try {
76 validationInterval = Long.parseLong(getPrefs().getPoolProperties()
77 .getProperty("validationInterval", "21600"));
78
79 validationInterval = validationInterval * 1000;
80 } catch (NumberFormatException ex) {
81 validationInterval = 21600000;
82 }
83
84 Date rightNow = new Date();
85
86
87 if (conNextValidationDate == null) {
88 conNextValidationDate = new Date(validationInterval + rightNow.getTime());
89 }
90
91 if (con != null && conNextValidationDate.before(rightNow)) {
92 conNextValidationDate.setTime(validationInterval
93 + rightNow.getTime());
94 String validationQuery = getPrefs().getPoolProperties().getProperty("validationQuery", "");
95 if (!Util.isNull(validationQuery)) {
96 logCat.debug("Testing connection: checking validation timestamp='"
97 + rightNow.toString() + "'.");
98 logCat.debug("Testing connection: next validation check='"
99 + conNextValidationDate.toString() + "'.");
100 logCat.debug("Testing connection: validationQuery='"
101 + validationQuery + "'.");
102
103 try {
104 Statement st = con.createStatement();
105 ResultSet rs = st.executeQuery(validationQuery);
106 try {
107 rs.next();
108 logCat.debug("Testing connection: Connection is valid.");
109 } finally {
110 rs.close();
111 st.close();
112 }
113 } catch (SQLException sqlex) {
114
115
116
117 logCat.debug("Testing connection: Connection is invalid. Forcing recreate.");
118 con.close();
119 con = null;
120 }
121 }
122 }
123
124 if (con == null) {
125 Properties props = getPrefs()
126 .getProperties();
127
128
129 if ((props != null) && !props.isEmpty()) {
130 props.put("user", getPrefs().getUser());
131 props.put("password", getPrefs().getPassword());
132 con = DriverManager.getConnection(getPrefs().getJdbcURL(), props);
133 }
134
135 else {
136 con = DriverManager.getConnection(getPrefs().getJdbcURL(),
137 getPrefs().getUser(),
138 getPrefs().getPassword());
139 }
140 }
141
142 return new SingleConnectionWrapper(con);
143 }
144
145
146 /***
147 * Initialize the ConnectionProvider.
148 *
149 * @throws Exception if any error occurs
150 */
151 protected void init() throws Exception {
152 Class.forName(getPrefs().getJdbcDriver())
153 .newInstance();
154 }
155 }