Skip to content

Commit a9d93f5

Browse files
committed
add Connection Pool interface
1 parent 890e660 commit a9d93f5

6 files changed

Lines changed: 248 additions & 15 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* @Title: ConnectionManager.java
20+
* @Package openthinks.libs.sql.dao
21+
* @Description: TODO
22+
* @author dailey.yet@outlook.com
23+
* @date Jul 30, 2015
24+
* @version V1.0
25+
*/
26+
package openthinks.libs.sql.dao;
27+
28+
import java.sql.Connection;
29+
import java.sql.DriverManager;
30+
import java.sql.SQLException;
31+
32+
import org.apache.log4j.Logger;
33+
34+
import openthinks.libs.sql.dao.pool.ConnectionPool;
35+
import openthinks.libs.sql.dao.pool.ConnectionPoolManager;
36+
import openthinks.libs.sql.lang.Configurator;
37+
import openthinks.libs.utilities.Checker;
38+
39+
/**
40+
* @author dailey.yet@outlook.com
41+
*
42+
*/
43+
public final class ConnectionManager {
44+
private static Logger logger = Logger.getLogger(ConnectionManager.class.getName());
45+
/**
46+
* @param configurator Configurator
47+
* @return Connection
48+
* @throws ClassNotFoundException
49+
* @throws SQLException
50+
*/
51+
public static Connection getConnection(Configurator configurator) throws ClassNotFoundException, SQLException {
52+
Checker.require(configurator).notNull();
53+
Connection conn = null;
54+
if(configurator.isUsePool()){
55+
ConnectionPool connPool=ConnectionPoolManager.getInstance(configurator);
56+
conn = connPool.request();
57+
}else{
58+
Class.forName(configurator.getDriver());
59+
conn = DriverManager.getConnection(configurator.getUrl(),
60+
configurator.getUserName(), configurator.getUserPwd());
61+
62+
}
63+
return conn;
64+
}
65+
66+
/**
67+
* @param configurator Configurator
68+
* @param conns Connection[]
69+
*/
70+
public static void closeConnection(Configurator configurator, Connection... conns) {
71+
if(conns==null || conns.length==0) return;
72+
Checker.require(configurator).notNull();
73+
if(configurator.isUsePool()){
74+
ConnectionPool connPool=ConnectionPoolManager.getInstance(configurator);
75+
connPool.recycle(conns);
76+
}else{
77+
for(Connection conn:conns){
78+
if (conn != null) {
79+
try {
80+
if (!conn.isClosed()) {
81+
conn.close();
82+
conn = null;
83+
}
84+
} catch (SQLException e) {
85+
logger.error(e);
86+
}
87+
}
88+
}
89+
90+
}
91+
}
92+
93+
94+
}

src/openthinks/libs/sql/dao/impl/BaseDaoImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.Collections;
1515
import java.util.List;
1616

17+
import org.apache.log4j.Logger;
18+
1719
import openthinks.libs.sql.dao.BaseDao;
1820
import openthinks.libs.sql.data.DefaultRow;
1921
import openthinks.libs.sql.data.Row;
@@ -25,8 +27,6 @@
2527
import openthinks.libs.sql.lang.reflect.ReflectEngine;
2628
import openthinks.libs.utilities.Checker;
2729

28-
import org.apache.log4j.Logger;
29-
3030
/**
3131
* BaseDao接口默认实现类
3232
*
@@ -445,6 +445,9 @@ public void closeConnection(Connection conn) {
445445
logger.error(e);
446446
}
447447
}
448+
if(conn != null && isUsePool() ){
449+
//TODO put it back to pool
450+
}
448451
}
449452

450453
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* @Title: ConnectionPool.java
20+
* @Package openthinks.libs.sql.dao.pool
21+
* @Description: TODO
22+
* @author dailey.yet@outlook.com
23+
* @date Jul 30, 2015
24+
* @version V1.0
25+
*/
26+
package openthinks.libs.sql.dao.pool;
27+
28+
import java.sql.Connection;
29+
30+
/**
31+
* The pool for {@link Connection}
32+
* @author dailey.yet@outlook.com
33+
*
34+
*/
35+
public interface ConnectionPool {
36+
37+
/**
38+
* request a {@link Connection}, maybe it is a new or it is already existed
39+
* @return Connection
40+
*/
41+
Connection request();
42+
43+
/**
44+
* recycle the {@link Connection}s
45+
* @param conns Connection[]
46+
*/
47+
void recycle(Connection... conns);
48+
49+
/**
50+
* really close the given {@link Connection}s
51+
* @param conns Connection[]
52+
*/
53+
void destroy(Connection... conns);
54+
55+
/**
56+
* current count of active {@link Connection} in pool
57+
* @return Integer
58+
*/
59+
int size();
60+
61+
/**
62+
* the capacity or max count of the pool for holding {@link Connection}
63+
* @return Integer
64+
*/
65+
int capacity();
66+
67+
/**
68+
* really close all holding {@link Connection}s
69+
*/
70+
void destroyAll();
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* @Title: ConnectionPoolManager.java
20+
* @Package openthinks.libs.sql.dao.pool
21+
* @Description: TODO
22+
* @author dailey.yet@outlook.com
23+
* @date Jul 30, 2015
24+
* @version V1.0
25+
*/
26+
package openthinks.libs.sql.dao.pool;
27+
28+
import openthinks.libs.sql.lang.Configurator;
29+
30+
/**
31+
* The management of {@link ConnectionPool}
32+
* @author dailey.yet@outlook.com
33+
*
34+
*/
35+
public class ConnectionPoolManager {
36+
37+
/**
38+
* @param configurator Configurator
39+
* @return ConnectionPool
40+
*/
41+
public static ConnectionPool getInstance(Configurator configurator) {
42+
// TODO Auto-generated method stub
43+
return null;
44+
}
45+
46+
}

src/openthinks/libs/sql/dhibernate/support/SessionDaoImpl.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.sql.Connection;
44
import java.sql.SQLException;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
57

8+
import openthinks.libs.sql.dao.ConnectionManager;
69
import openthinks.libs.sql.dao.impl.BaseDaoImpl;
710

811
/**
@@ -17,6 +20,7 @@ class SessionDaoImpl extends BaseDaoImpl {
1720
*/
1821
private Connection connection = null;
1922
private Boolean autoClose = true;
23+
private Lock lock = new ReentrantLock();
2024

2125
/**
2226
* @param autoClose
@@ -46,9 +50,14 @@ public SessionDaoImpl() {
4650
*/
4751
@Override
4852
public Connection getConn() throws ClassNotFoundException, SQLException {
49-
if (connection == null)
50-
connection = super.getConn();
51-
return connection;
53+
lock.lock();
54+
try {
55+
if (connection == null)
56+
connection = ConnectionManager.getConnection(getConfigurator());
57+
return connection;
58+
} finally {
59+
lock.unlock();
60+
}
5261
}
5362

5463
/**
@@ -59,17 +68,27 @@ public Connection getConn() throws ClassNotFoundException, SQLException {
5968
*/
6069
@Override
6170
public void setConn(Connection connection) {
62-
closeConnection(this.connection);
63-
this.connection = connection;
71+
lock.lock();
72+
try {
73+
closeConnection(this.connection);
74+
this.connection = connection;
75+
} finally {
76+
lock.unlock();
77+
}
6478
}
6579

6680
@Override
6781
public void closeConnection(Connection conn) {
68-
if (isAutoClose())
69-
super.closeConnection(conn);
70-
if (isUsePool()) {
71-
// this.connection = null;
72-
//TODO put back it to pool
82+
lock.lock();
83+
try {
84+
if (isAutoClose())
85+
super.closeConnection(conn);
86+
if (isUsePool()) {
87+
ConnectionManager.closeConnection(getConfigurator(), conn);
88+
conn = null;
89+
}
90+
} finally {
91+
lock.unlock();
7392
}
7493

7594
}

src/openthinks/libs/sql/dhibernate/support/query/Filters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public static AbstractQueryFilter<ContainerFilter> include(Object value) {
5151
return new ContainerFilter().include(value);
5252
}
5353

54-
public static AbstractQueryFilter<EqualsFilter> eq(String attributeName, Object value) {
54+
public static QueryFilter eq(String attributeName, Object value) {
5555
return new EqualsFilter(null, attributeName).eq(value);
5656
}
5757

58-
public static AbstractQueryFilter<NotEqualsFilter> neq(String attributeName, Object value) {
58+
public static QueryFilter neq(String attributeName, Object value) {
5959
return new NotEqualsFilter(null, attributeName).neq(value);
6060
}
6161

62-
public static AbstractQueryFilter<ContainerFilter> include(String attributeName, Object value) {
62+
public static QueryFilter include(String attributeName, Object value) {
6363
return new ContainerFilter(null, attributeName).include(value);
6464
}
6565

0 commit comments

Comments
 (0)