-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSQLPool.java
More file actions
70 lines (59 loc) · 1.77 KB
/
Copy pathSQLPool.java
File metadata and controls
70 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package javaforce;
import java.sql.*;
import org.apache.tomcat.jdbc.pool.*;
/** SQLPool based on Tomcat Connection Pool.
*
* NOTE : If using in standalone project you must include tomcat jar files.
* These jar files are renamed during the download:
* tomcat-jdbc-X,Y.Z.jar -> jdbc-api.jar (ant task jar-jdbc-api)
* tomcat-juli-X.Y.Z.jar -> juli-api.jar (ant task jar-juli-api)
*
* @author pquiring
*/
public class SQLPool {
private DataSource dataSource;
/** Init SQL connection pool.
*
* This should be done in your servlets init() method.
*/
public boolean init(String jdbcClass, String connectionURL) {
// 1. Configure Pool Properties
PoolProperties p = new PoolProperties();
p.setUrl(connectionURL);
p.setDriverClassName(jdbcClass);
// p.setUsername("myuser");
// p.setPassword("mypassword");
// Optional: Pool tuning
p.setMaxActive(20);
p.setMaxIdle(10);
p.setMinIdle(5);
p.setInitialSize(5);
p.setValidationQuery("SELECT 1");
p.setTestOnBorrow(true);
// 2. Create the DataSource
dataSource = new DataSource();
dataSource.setPoolProperties(p);
return true;
}
/** Allocate an raw java SQL connection. */
public Connection getConnection() throws SQLException {
if (dataSource == null) return null;
return dataSource.getConnection();
}
/** Allocate an SQL connection in a javaforce.SQL class. */
public SQL getSQL() throws SQLException {
Connection conn = getConnection();
if (conn == null) return null;
return new SQL(conn);
}
/** Close the connection pool.
*
* This should be done in your servlets destroy() method or leaks may occur.
*/
public void close() {
if (dataSource != null) {
dataSource.close();
dataSource = null;
}
}
}