19
19
import java .sql .PreparedStatement ;
20
20
import java .sql .ResultSet ;
21
21
import java .sql .SQLException ;
22
+ import java .util .Collection ;
23
+ import java .util .HashMap ;
22
24
import java .util .List ;
25
+ import java .util .Map ;
23
26
24
27
import javax .validation .constraints .NotNull ;
25
28
@@ -67,14 +70,15 @@ public int executeUpdate(@NotNull SQLConfig config) throws Exception {
67
70
68
71
//TODO 根据不同数据库来分组存 connection 和 statement,例如 Map<database, connection>,
69
72
// 解决一次请求中有2个以上不同数据库类型导致后面的查询都用第一个的数据库类型
70
- private Connection connection = null ;
71
- private PreparedStatement statement = null ;
73
+ private Map <String , Connection > connectionMap = new HashMap <>();
72
74
/**
73
75
* @param config
74
76
* @return
75
77
* @throws Exception
76
78
*/
79
+ @ SuppressWarnings ("resource" )
77
80
private PreparedStatement getStatement (@ NotNull SQLConfig config ) throws Exception {
81
+ Connection connection = connectionMap .get (config .getDatabase ());
78
82
if (connection == null || connection .isClosed ()) {
79
83
Log .i (TAG , "select connection " + (connection == null ? " = null" : ("isClosed = " + connection .isClosed ()))) ;
80
84
@@ -85,9 +89,10 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
85
89
connection = DriverManager .getConnection (config .getDBUri () + "?useUnicode=true&characterEncoding=UTF-8&user="
86
90
+ config .getDBAccount () + "&password=" + config .getDBPassword ());
87
91
}
92
+ connectionMap .put (config .getDatabase (), connection );
88
93
}
89
94
90
- statement = connection .prepareStatement (config .getSQL (config .isPrepared ())); //创建Statement对象
95
+ PreparedStatement statement = connection .prepareStatement (config .getSQL (config .isPrepared ())); //创建Statement对象
91
96
List <Object > valueList = config .isPrepared () ? config .getPreparedValueList () : null ;
92
97
93
98
if (valueList != null && valueList .isEmpty () == false ) {
@@ -102,6 +107,7 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
102
107
}
103
108
}
104
109
}
110
+ // statement.close();
105
111
106
112
return statement ;
107
113
}
@@ -112,20 +118,27 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
112
118
@ Override
113
119
public void close () {
114
120
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
+ }
121
137
}
122
- } catch (SQLException e ) {
123
- e .printStackTrace ();
124
138
}
125
- statement = null ;
126
- connection = null ;
127
- }
128
-
129
139
140
+ connectionMap .clear ();
141
+ connectionMap = null ;
142
+ }
130
143
131
144
}
0 commit comments