/************** private stuff ***************/ private void closeConnectionIfNecessary(){ try{ if (connection.autoClose){ connection.close(); } } catch (Exception ex){ throw new Sql2oException("Error while attempting to close connection", ex); } }
@Override public void close() { try { if (rs != null) { rs.close(); // log the query long afterClose = System.currentTimeMillis(); logger.debug("total: {} ms, execution: {} ms, reading and parsing: {} ms; executed [{}]", new Object[]{ afterClose - start, afterExecQuery-start, afterClose - afterExecQuery, name }); rs = null; } } catch (SQLException ex) { throw new Sql2oException("Error closing ResultSet.", ex); } finally { if (this.isAutoCloseConnection()){ connection.close(); } else { closeConnectionIfNecessary(); } } }
/** * Begins a transaction with the given isolation level. Every statement executed on the return {@link Connection} * instance, will be executed in the transaction. It is very important to always call either the {@link org.sql2o.Connection#commit()} * method or the {@link org.sql2o.Connection#rollback()} method to close the transaction. Use proper try-catch logic. * @param connectionSource the {@link ConnectionSource} implementation substitution, * that will be used instead of one from {@link Sql2o} instance. * @param isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(ConnectionSource connectionSource, int isolationLevel) { Connection connection = new Connection(this, connectionSource, false); boolean success = false; try { connection.getJdbcConnection().setAutoCommit(false); connection.getJdbcConnection().setTransactionIsolation(isolationLevel); success = true; } catch (SQLException e) { throw new Sql2oException("Could not start the transaction - " + e.getMessage(), e); } finally { if (!success) { connection.close(); } } return connection; }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument */ public void withConnection(StatementRunnable runnable, Object argument) { Connection connection = null; try{ connection = open(); runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally{ if (connection != null) { connection.close(); } } }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument * @param <V> * @return */ @SuppressWarnings("unchecked") public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument) { Connection connection = null; try{ connection = open(); return (V)runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } }
/************** private stuff ***************/ private void closeConnectionIfNecessary() { try { if (connection.autoClose) { connection.close(); } } catch (Exception ex) { throw new Sql2oException("Error while attempting to close connection", ex); } }
/************** private stuff ***************/ private void closeConnectionIfNecessary(){ try{ if (connection.autoClose){ connection.close(); } } catch (Exception ex){ throw new Sql2oException("Error while attempting to close connection", ex); } }
private void closeConn(Connection connection) { ifThen(localConnection.get() == null && connection != null, () -> connection.close()); }
/** * Clear the battlefield after a database operation. * * @param conn sql2o connection */ private void clean(Connection conn) { this.selectColumns = null; this.isSQLLimit = false; this.orderBySQL = new StringBuilder(); this.conditionSQL = new StringBuilder(); this.paramValues.clear(); this.excludedColumns.clear(); this.updateColumns.clear(); ifThen(localConnection.get() == null && conn != null, () -> conn.close()); }
public <T> List<T> list(Class<T> type) { List<T> result = null; Query query = null; String sql = buildSql(this.customSql); sql = sql.replaceAll("#table#", Util.getclassName(type).toLowerCase()); query = connection.createQuery(sql); logger.info(Constant.LOG_AC_NAME+" execute sql " + sql ); result = query.executeAndFetch(type); this.connection.close(); return result; }
@Override public void close() { try { if (rs != null) { rs.close(); // log the query if (Anima.of().isEnableSQLStatistic() && log.isDebugEnabled()) { long afterClose = System.currentTimeMillis(); log.debug("Total => {} ms, execution: {} ms, reading and parsing: {} ms; executed [{}]", afterClose - start, afterExecQuery - start, afterClose - afterExecQuery, name); } rs = null; } } catch (SQLException ex) { throw new Sql2oException("Error closing ResultSet.", ex); } finally { if (this.isAutoCloseConnection()) { connection.close(); } else { closeConnectionIfNecessary(); } } }
public static <T> List<T> getList(String sql, Class<T> clazz, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); List<T> list = query.executeAndFetch(clazz); con.close(); return list; }
/** * Invokes the run method on the {@link StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * * @param runnable * @param argument * @param <V> * @return */ public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument) { Connection connection = null; try { connection = open(); return (V) runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument */ public void withConnection(StatementRunnable runnable, Object argument) { Connection connection = null; try{ connection = open(); runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally{ if (connection != null) { connection.close(); } } }
@SuppressWarnings("unchecked") public static Map<String, Object> getMap(String sql, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); Map<String, Object> t = (Map<String, Object>) query.executeScalar(); con.close(); return t; }
/** * Invokes the run method on the {@link StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * * @param runnable * @param argument */ public void withConnection(StatementRunnable runnable, Object argument) { Connection connection = null; try { connection = open(); runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } }
public static <T> T get(String sql, Class<T> clazz, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); T t = query.executeAndFetchFirst(clazz); con.close(); return t; }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument * @param <V> * @return */ @SuppressWarnings("unchecked") public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument) { Connection connection = null; try{ connection = open(); return (V)runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } }
public static List<Map<String, Object>> getMapList(String sql, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); List<Map<String, Object>> t = query.executeAndFetchTable().asList(); con.close(); return t; }
/** * 带参数更新 * @param sql * @param params * @return */ public static int update(String sql, Map<String, Object> params){ Connection con = sql2o.open(); Query query = con.createQuery(sql); executeQuery(query, params); int res = query.executeUpdate().getResult(); con.close(); return res; }