/** * 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; }
try { if (columnNames != null && columnNames.length > 0){ preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames); } else if (returnGeneratedKeys) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); } else { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery);
private PreparedStatement buildPreparedStatement(boolean allowArrayParameters) { log.debug("Parameters => {}", paramIndexValues.values()); // prepare statement creation if (preparedStatement == null) { try { if (columnNames != null && columnNames.length > 0) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames); } else if (returnGeneratedKeys) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); } else { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery); } } catch (SQLException ex) { throw new Sql2oException(String.format("Error preparing statement - %s", ex.getMessage()), ex); } connection.registerStatement(preparedStatement); } // parameters assignation to query for (Map.Entry<Integer, Object> entry : paramIndexValues.entrySet()) { try { getQuirks().setParameter(preparedStatement, entry.getKey(), entry.getValue()); } catch (SQLException e) { throw new RuntimeException(String.format("Error adding parameter '%s' - %s", entry.getKey(), e.getMessage()), e); } } paramIndexValues.clear(); return preparedStatement; }
/** * 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; }
/** * 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 Connection#commit()} * method or the {@link 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; }
try { if (columnNames != null && columnNames.length > 0){ preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames); } else if (returnGeneratedKeys) { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); } else { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery);
public <S extends Serializable> S save() { QueryMeta queryMeta = SqlBuilder.buildInsertSql(this); Connection con = getConn(); log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", this); Query query = con.createQuery(queryMeta.getSql()).bind(this); try { S s = (S) query.executeUpdate().getKey(); if (null == Base.connectionThreadLocal.get() && !con.getJdbcConnection().getAutoCommit()) { con.commit(); } return s; } catch (Exception e) { throw new RuntimeException(e); } }
private int invoke(QueryMeta queryMeta) { log.debug(EXECUTE_SQL_PREFIX + " => {}", queryMeta.getSql()); log.debug(PARAMETER_PREFIX + " => {}", Arrays.toString(queryMeta.getParams())); Connection con = getConn(); Query query = con.createQuery(queryMeta.getSql()).withParams(queryMeta.getParams()); if (queryMeta.hasColumnMapping()) { queryMeta.getColumnMapping().forEach(query::addColumnMapping); } int result = query.executeUpdate().getResult(); try { if (null == Base.connectionThreadLocal.get() && !con.getJdbcConnection().getAutoCommit()) { con.commit(); } } catch (Exception e) { throw new RuntimeException(e); } return result; }