Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / JDBCUtils.java @ 47784

History | View | Annotate | Download (11.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.jdbc2;
25

    
26
import java.sql.Connection;
27
import java.sql.PreparedStatement;
28
import java.sql.ResultSet;
29
import java.sql.SQLException;
30
import java.sql.Statement;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Objects;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
@SuppressWarnings("UseSpecificCatch")
38
public class JDBCUtils {
39

    
40
    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class);
41
    
42
    private JDBCUtils() {
43

    
44
    }
45

    
46
    private static String getSQLInfo(PreparedStatement st, String sql) {
47
        try {
48
            return sql+" [[ "+Objects.toString(st)+" ]]";
49
        } catch(Throwable th) {
50
            return sql;
51
        }
52
    }
53

    
54
    public static String getConnId(Statement st) {
55
        if( st==null ) {
56
            return "null";
57
        }
58
        try {
59
            return getHexId(st.getConnection());
60
        } catch (SQLException ex) {
61
            return "null";
62
        }
63
    }
64

    
65
    public static String getConnId(ResultSet resulSet) {
66
        if( resulSet==null ) {
67
            return "null";
68
        }
69
        try {
70
            return getConnId(resulSet.getStatement());
71
        } catch (SQLException ex) {
72
            return "null";
73
        }
74
    }
75

    
76
    public static String getConnId(Connection conn) {
77
        return getHexId(conn);
78
    }
79
    
80
    public static String getHexId(Object obj) {
81
        if( obj==null ) {
82
            return "null";
83
        }
84
        return Integer.toHexString(obj.hashCode()).toUpperCase();
85
    }
86
    
87
    public static ResultSet executeQuery(Statement st, String sql) throws SQLException {
88
        ResultSet rs;
89
        try {
90
            if( LOGGER.isDebugEnabled() ) {
91
                long t1 = System.currentTimeMillis();
92
                rs = st.executeQuery(sql);
93
                long t2 = System.currentTimeMillis();
94
                LOGGER.debug("["+getConnId(st)+"] executeQuery(st) "+(t2-t1)+"ms SQL= " + sql);
95
            } else {
96
                rs = st.executeQuery(sql);
97
            }
98
            return rs;
99
        } catch(Exception ex) {
100
            LOGGER.debug("execute SQL: " + sql, ex);
101
            throw ex;
102
        }
103
    }
104

    
105
    public static void execute(Statement st, String sql) throws SQLException {
106
        try {
107
            if( LOGGER.isDebugEnabled() ) {
108
                long t1 = System.currentTimeMillis();
109
                st.execute(sql);
110
                long t2 = System.currentTimeMillis();
111
                LOGGER.debug("["+getConnId(st)+"] execute(st) "+(t2-t1)+"ms SQL: " + sql);
112
            } else {
113
                st.execute(sql);
114
            }
115
        } catch(Exception ex) {
116
            LOGGER.debug("execute SQL: " + sql, ex);
117
            throw ex;
118
        }
119
    }
120

    
121
    public static void execute(Connection connection, String sql) throws SQLException {
122
        try {
123
            Statement st = connection.createStatement();
124
            if( LOGGER.isDebugEnabled() ) {
125
                long t1 = System.currentTimeMillis();
126
                st.execute(sql);
127
                long t2 = System.currentTimeMillis();
128
                LOGGER.debug("["+getConnId(connection)+"] execute(conn) "+(t2-t1)+"ms SQL: " + sql);
129
            } else {
130
                st.execute(sql);
131
            }
132
        } catch(Exception ex) {
133
            LOGGER.debug("execute SQL: " + sql, ex);
134
            throw ex;
135
        }
136
    }
137

    
138
    public static void execute(Connection connection, List<String> sqls) throws SQLException {
139
        Statement st = null;
140
        String sql = null;
141
        try {
142
            st = connection.createStatement();
143
            Iterator<String> it = sqls.iterator();
144
            while( it.hasNext() ) {
145
                sql = it.next();
146
                if( LOGGER.isDebugEnabled() ) {
147
                    long t1 = System.currentTimeMillis();
148
                    st.execute(sql);
149
                    long t2 = System.currentTimeMillis();
150
                    LOGGER.debug("["+getConnId(connection)+"] execute(conn, sqls) "+(t2-t1)+"ms SQLs: " + sql);
151
                } else {
152
                    st.execute(sql);
153
                }
154
            }
155
        } catch (SQLException ex) {
156
            LOGGER.debug("execute SQL: " + sql, ex);
157
            throw ex;
158
        } finally {
159
            JDBCUtils.closeQuietly(st);
160
        }       
161
    }
162

    
163
    public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException {
164
        ResultSet rs;
165
        try {
166
            if( LOGGER.isDebugEnabled() ) {
167
                long t1 = System.currentTimeMillis();
168
                rs = st.executeQuery();
169
                long t2 = System.currentTimeMillis();
170
                LOGGER.debug("["+getConnId(st)+"] executeQuery(pst) "+(t2-t1)+"ms SQL= "+ getSQLInfo(st,sql));
171
            } else {
172
                rs = st.executeQuery();
173
            }
174
            return rs;
175
        } catch(Exception ex) {
176
            LOGGER.debug("execute query SQL: " + getSQLInfo(st,sql), ex);
177
            throw new SQLException(ex.getMessage() +getSQLInfo(st,sql), ex);
178
        }
179
    }
180

    
181
    public static void execute(PreparedStatement st, String sql) throws SQLException {
182
        try {
183
            if( LOGGER.isDebugEnabled() ) {
184
                long t1 = System.currentTimeMillis();
185
                st.execute();
186
                long t2 = System.currentTimeMillis();
187
                LOGGER.debug("["+getConnId(st)+"] execute(pst) "+(t2-t1)+"ms SQL= "+ getSQLInfo(st,sql));
188
            } else {
189
                st.execute();
190
            }
191
        } catch(Exception ex) {
192
            LOGGER.debug("execute SQL: " + getSQLInfo(st,sql), ex);
193
            throw new SQLException(ex.getMessage() +getSQLInfo(st,sql), ex);
194
        }
195
    }
196

    
197
    public static int executeUpdate(PreparedStatement st, String sql) throws SQLException {
198
        int r;
199
        try {
200
            if( LOGGER.isDebugEnabled() ) {
201
                long t1 = System.currentTimeMillis();
202
                r = st.executeUpdate();
203
                long t2 = System.currentTimeMillis();
204
                LOGGER.debug("["+getConnId(st)+"] executeUpdate(pst) "+(t2-t1)+"ms SQL= "+ getSQLInfo(st,sql));
205
            } else {
206
                r = st.executeUpdate();
207
            }
208
            return r;
209
        } catch(Exception ex) {
210
            LOGGER.debug("execute update SQL: " + getSQLInfo(st,sql), ex);
211
            throw new SQLException(ex.getMessage() +getSQLInfo(st,sql), ex);
212
        }
213
    }
214

    
215
    public static int executeUpdate(Statement st, String sql) throws SQLException {
216
        int r;
217
        try {
218
            if( LOGGER.isDebugEnabled() ) {
219
                long t1 = System.currentTimeMillis();
220
                r = st.executeUpdate(sql);
221
                long t2 = System.currentTimeMillis();
222
                LOGGER.debug("["+getConnId(st)+"] executeUpdate(st) "+(t2-t1)+"ms SQL= "+ sql);
223
            } else {
224
                r = st.executeUpdate(sql);
225
            }
226
            return r;
227
        } catch(Exception ex) {
228
            LOGGER.debug("execute update SQL: " + sql, ex);
229
            throw ex;
230
        }
231
    }
232

    
233
    public static int[] executeBatch(PreparedStatement st, String sql) throws SQLException {
234
        int[] r;
235
        try {
236
            if( LOGGER.isDebugEnabled() ) {
237
                long t1 = System.currentTimeMillis();
238
                r = st.executeBatch();
239
                long t2 = System.currentTimeMillis();
240
                LOGGER.debug("["+getConnId(st)+"] executeBatch(pst) "+(t2-t1)+"ms SQL= "+ getSQLInfo(st,sql));
241
            } else {
242
                r = st.executeBatch();
243
            }
244
            return r;
245
        } catch(Exception ex) {
246
            LOGGER.debug("execute batch SQL: " + getSQLInfo(st,sql), ex);
247
            throw ex;
248
        }
249
    }
250

    
251
    public static void addBatch(PreparedStatement st, String sql) throws SQLException {
252
        try {
253
            if( LOGGER.isDebugEnabled() ) {
254
                long t1 = System.currentTimeMillis();
255
                st.addBatch();
256
                long t2 = System.currentTimeMillis();
257
                LOGGER.debug("["+getConnId(st)+"] addBatch(pst) "+(t2-t1)+"ms SQL= "+ getSQLInfo(st,sql));
258
            } else {
259
                st.addBatch();
260
            }
261
        } catch(Exception ex) {
262
            LOGGER.debug("add batch SQL: " + getSQLInfo(st,sql), ex);
263
            throw ex;
264
        }
265
    }
266

    
267
    public static void closeQuietly(Statement st) {
268
//        if( LOGGER.isDebugEnabled() ) {
269
//            LOGGER.debug("["+getConnId(st)+"] Close statement");
270
//        }
271
        if (st == null) {
272
            return;
273
        }
274
        try {
275
            st.close();
276
        } catch (Exception e) {
277
            LOGGER.debug("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e);
278
        }
279
    }
280

    
281
    public static void closeQuietly(ResultSet resulSet) {
282
//        if( LOGGER.isDebugEnabled() ) {
283
//            LOGGER.debug("["+getConnId(resulSet)+"] Close ResultSet");
284
//        }
285
        if (resulSet == null) {
286
            return;
287
        }
288
        try {
289
            resulSet.close();
290
        } catch (Exception e) {
291
            LOGGER.debug("Problems closing " + resulSet.getClass().getSimpleName() + " '" + resulSet.toString() + "'.", e);
292
        }
293
    }
294

    
295
    public static void closeQuietly(Connection conn) {
296
//        if( LOGGER.isDebugEnabled() ) {
297
//            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
298
//        }
299
        if (conn == null) {
300
            return;
301
        }
302
        try {
303
            conn.close();
304
        } catch (Exception e) {
305
            LOGGER.debug("Problems closing " + conn.getClass().getSimpleName() + " '" + conn.toString() + "'.", e);
306
        }
307
    }
308
    
309
    public static void close(Connection conn) {
310
//        if( LOGGER.isDebugEnabled() ) {
311
//            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
312
//        }
313
        if (conn == null) {
314
            return;
315
        }
316
        try {
317
            conn.close();
318
        } catch (Exception e) {
319
            throw new RuntimeException(e);
320
        }
321
    }
322
    
323
    public static void closeQuietly(AutoCloseable obj) {
324
        if (obj == null) {
325
            return;
326
        }
327
        try {
328
//            if( LOGGER.isDebugEnabled() ) {
329
//                LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
330
//            }
331
            obj.close();
332
        } catch (Exception e) {
333
            LOGGER.debug("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
334
        }
335
    }
336
}