Revision 45097 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
JDBCUtils.java | ||
---|---|---|
50 | 50 |
return sql; |
51 | 51 |
} |
52 | 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 |
} |
|
53 | 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 |
|
|
54 | 87 |
public static ResultSet executeQuery(Statement st, String sql) throws SQLException { |
55 |
LOGGER.debug("execute query SQL: " + sql); |
|
88 |
if( LOGGER.isDebugEnabled() ) { |
|
89 |
LOGGER.debug("["+getConnId(st)+"] executeQuery(st) SQL= " + sql); |
|
90 |
} |
|
56 | 91 |
try { |
92 |
@SuppressWarnings("null") |
|
57 | 93 |
ResultSet rs = st.executeQuery(sql); |
58 | 94 |
return rs; |
59 | 95 |
} catch(Exception ex) { |
... | ... | |
62 | 98 |
} |
63 | 99 |
} |
64 | 100 |
|
101 |
@SuppressWarnings("null") |
|
65 | 102 |
public static void execute(Statement st, String sql) throws SQLException { |
66 |
LOGGER.debug("execute SQL: " + sql); |
|
103 |
if( LOGGER.isDebugEnabled() ) { |
|
104 |
LOGGER.debug("["+getConnId(st)+"] execute(st) SQL: " + sql); |
|
105 |
} |
|
67 | 106 |
try { |
68 | 107 |
st.execute(sql); |
69 | 108 |
} catch(Exception ex) { |
... | ... | |
73 | 112 |
} |
74 | 113 |
|
75 | 114 |
public static void execute(Connection connection, String sql) throws SQLException { |
76 |
LOGGER.debug("execute SQL: " + sql); |
|
77 |
Statement st = connection.createStatement(); |
|
115 |
if( LOGGER.isDebugEnabled() ) { |
|
116 |
LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQL: " + sql); |
|
117 |
} |
|
78 | 118 |
try { |
119 |
@SuppressWarnings("null") |
|
120 |
Statement st = connection.createStatement(); |
|
79 | 121 |
st.execute(sql); |
80 | 122 |
} catch(Exception ex) { |
81 | 123 |
LOGGER.warn("execute SQL: " + sql, ex); |
... | ... | |
91 | 133 |
Iterator<String> it = sqls.iterator(); |
92 | 134 |
while( it.hasNext() ) { |
93 | 135 |
sql = it.next(); |
94 |
LOGGER.debug("execute SQL: " + sql); |
|
136 |
if( LOGGER.isDebugEnabled() ) { |
|
137 |
LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQLs: " + sql); |
|
138 |
} |
|
95 | 139 |
st.execute(sql); |
96 | 140 |
} |
97 | 141 |
} catch (SQLException ex) { |
... | ... | |
104 | 148 |
|
105 | 149 |
public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
106 | 150 |
if( LOGGER.isDebugEnabled() ) { |
107 |
LOGGER.debug("execute query SQL= "+ getSQLInfo(st,sql));
|
|
151 |
LOGGER.debug("["+getConnId(st)+"] executeQuery(pst) SQL= "+ getSQLInfo(st,sql));
|
|
108 | 152 |
} |
109 | 153 |
try { |
154 |
@SuppressWarnings("null") |
|
110 | 155 |
ResultSet rs = st.executeQuery(); |
111 | 156 |
return rs; |
112 | 157 |
} catch(Exception ex) { |
... | ... | |
115 | 160 |
} |
116 | 161 |
} |
117 | 162 |
|
163 |
@SuppressWarnings("null") |
|
118 | 164 |
public static void execute(PreparedStatement st, String sql) throws SQLException { |
119 | 165 |
if( LOGGER.isDebugEnabled() ) { |
120 |
LOGGER.debug("execute SQL= "+ getSQLInfo(st,sql));
|
|
166 |
LOGGER.debug("["+getConnId(st)+"] execute(pst) SQL= "+ getSQLInfo(st,sql));
|
|
121 | 167 |
} |
122 | 168 |
try { |
123 | 169 |
st.execute(); |
... | ... | |
129 | 175 |
|
130 | 176 |
public static int executeUpdate(PreparedStatement st, String sql) throws SQLException { |
131 | 177 |
if( LOGGER.isDebugEnabled() ) { |
132 |
LOGGER.debug("execute update SQL= "+ getSQLInfo(st,sql));
|
|
178 |
LOGGER.debug("["+getConnId(st)+"] executeUpdate(pst) SQL= "+ getSQLInfo(st,sql));
|
|
133 | 179 |
} |
134 | 180 |
try { |
135 | 181 |
return st.executeUpdate(); |
... | ... | |
139 | 185 |
} |
140 | 186 |
} |
141 | 187 |
|
142 |
public static void closeQuietly(Statement obj) { |
|
143 |
if (obj == null) { |
|
188 |
public static void closeQuietly(Statement st) { |
|
189 |
if( LOGGER.isDebugEnabled() ) { |
|
190 |
LOGGER.debug("["+getConnId(st)+"] Close statement"); |
|
191 |
} |
|
192 |
if (st == null) { |
|
144 | 193 |
return; |
145 | 194 |
} |
146 | 195 |
try { |
147 |
obj.close();
|
|
196 |
st.close();
|
|
148 | 197 |
} catch (Exception e) { |
149 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
|
|
198 |
LOGGER.warn("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e);
|
|
150 | 199 |
} |
151 | 200 |
} |
152 | 201 |
|
153 |
public static void closeQuietly(ResultSet obj) { |
|
154 |
if (obj == null) { |
|
202 |
public static void closeQuietly(ResultSet resulSet) { |
|
203 |
if( LOGGER.isDebugEnabled() ) { |
|
204 |
LOGGER.debug("["+getConnId(resulSet)+"] Close ResultSet"); |
|
205 |
} |
|
206 |
if (resulSet == null) { |
|
155 | 207 |
return; |
156 | 208 |
} |
157 | 209 |
try { |
158 |
obj.close();
|
|
210 |
resulSet.close();
|
|
159 | 211 |
} catch (Exception e) { |
160 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
|
|
212 |
LOGGER.warn("Problems closing " + resulSet.getClass().getSimpleName() + " '" + resulSet.toString() + "'.", e);
|
|
161 | 213 |
} |
162 | 214 |
} |
163 | 215 |
|
164 | 216 |
public static void closeQuietly(Connection conn) { |
217 |
if( LOGGER.isDebugEnabled() ) { |
|
218 |
LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")"); |
|
219 |
} |
|
165 | 220 |
if (conn == null) { |
166 | 221 |
return; |
167 | 222 |
} |
168 |
LOGGER.trace("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")"); |
|
169 | 223 |
try { |
170 | 224 |
conn.close(); |
171 | 225 |
} catch (Exception e) { |
... | ... | |
173 | 227 |
} |
174 | 228 |
} |
175 | 229 |
|
230 |
public static void close(Connection conn) { |
|
231 |
if( LOGGER.isDebugEnabled() ) { |
|
232 |
LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")"); |
|
233 |
} |
|
234 |
if (conn == null) { |
|
235 |
return; |
|
236 |
} |
|
237 |
try { |
|
238 |
conn.close(); |
|
239 |
} catch (Exception e) { |
|
240 |
throw new RuntimeException(e); |
|
241 |
} |
|
242 |
} |
|
243 |
|
|
176 | 244 |
public static void closeQuietly(AutoCloseable obj) { |
177 | 245 |
if (obj == null) { |
178 | 246 |
return; |
179 | 247 |
} |
180 | 248 |
try { |
181 |
LOGGER.trace("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")"); |
|
249 |
if( LOGGER.isDebugEnabled() ) { |
|
250 |
LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")"); |
|
251 |
} |
|
182 | 252 |
obj.close(); |
183 | 253 |
} catch (Exception e) { |
184 | 254 |
LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
Also available in: Unified diff