1919import java .sql .PreparedStatement ;
2020import java .sql .ResultSet ;
2121import java .sql .SQLException ;
22+ import java .util .Collection ;
23+ import java .util .HashMap ;
2224import java .util .List ;
25+ import java .util .Map ;
2326
2427import javax .validation .constraints .NotNull ;
2528
@@ -67,14 +70,15 @@ public int executeUpdate(@NotNull SQLConfig config) throws Exception {
6770
6871 //TODO 根据不同数据库来分组存 connection 和 statement,例如 Map<database, connection>,
6972 // 解决一次请求中有2个以上不同数据库类型导致后面的查询都用第一个的数据库类型
70- private Connection connection = null ;
71- private PreparedStatement statement = null ;
73+ private Map <String , Connection > connectionMap = new HashMap <>();
7274 /**
7375 * @param config
7476 * @return
7577 * @throws Exception
7678 */
79+ @ SuppressWarnings ("resource" )
7780 private PreparedStatement getStatement (@ NotNull SQLConfig config ) throws Exception {
81+ Connection connection = connectionMap .get (config .getDatabase ());
7882 if (connection == null || connection .isClosed ()) {
7983 Log .i (TAG , "select connection " + (connection == null ? " = null" : ("isClosed = " + connection .isClosed ()))) ;
8084
@@ -85,9 +89,10 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
8589 connection = DriverManager .getConnection (config .getDBUri () + "?useUnicode=true&characterEncoding=UTF-8&user="
8690 + config .getDBAccount () + "&password=" + config .getDBPassword ());
8791 }
92+ connectionMap .put (config .getDatabase (), connection );
8893 }
8994
90- statement = connection .prepareStatement (config .getSQL (config .isPrepared ())); //创建Statement对象
95+ PreparedStatement statement = connection .prepareStatement (config .getSQL (config .isPrepared ())); //创建Statement对象
9196 List <Object > valueList = config .isPrepared () ? config .getPreparedValueList () : null ;
9297
9398 if (valueList != null && valueList .isEmpty () == false ) {
@@ -102,6 +107,7 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
102107 }
103108 }
104109 }
110+ // statement.close();
105111
106112 return statement ;
107113 }
@@ -112,20 +118,27 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
112118 @ Override
113119 public void close () {
114120 super .close ();
115- try {
116- if (statement != null && statement .isClosed () == false ) {
117- statement .close ();
118- }
119- if (connection != null && connection .isClosed () == false ) {
120- connection .close ();
121+
122+ if (connectionMap == null ) {
123+ return ;
124+ }
125+
126+ Collection <Connection > connections = connectionMap .values ();
127+
128+ if (connections != null ) {
129+ for (Connection connection : connections ) {
130+ try {
131+ if (connection != null && connection .isClosed () == false ) {
132+ connection .close ();
133+ }
134+ } catch (SQLException e ) {
135+ e .printStackTrace ();
136+ }
121137 }
122- } catch (SQLException e ) {
123- e .printStackTrace ();
124138 }
125- statement = null ;
126- connection = null ;
127- }
128-
129139
140+ connectionMap .clear ();
141+ connectionMap = null ;
142+ }
130143
131144}
0 commit comments