/** * Opens a connection to the database * @return instance of the {@link org.sql2o.Connection} class. */ public Connection open() { return new Connection(this, false); }
/** * Opens a connection to the database * @param connectionSource the {@link ConnectionSource} implementation substitution, * that will be used instead of one from {@link Sql2o} instance. * @return instance of the {@link org.sql2o.Connection} class. */ public Connection open(ConnectionSource connectionSource) { return new Connection(this, connectionSource, false); }
/** * Creates a {@link Query} * @param query the sql query string * @param returnGeneratedKeys boolean value indicating if the database should return any generated keys. * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query, boolean returnGeneratedKeys) { return new Connection(this, true).createQuery(query, returnGeneratedKeys); }
/** * Creates a {@link Query} * @param query the sql query string * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query){ Connection connection = new Connection(this, true); return connection.createQuery(query); }
/** * 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; }
/** * Opens a connection to the database * @param connectionSource the {@link ConnectionSource} implementation substitution, * that will be used instead of one from {@link Sql2o} instance. * @return instance of the {@link org.sql2o.Connection} class. */ public Connection open(ConnectionSource connectionSource) { return new Connection(this, connectionSource, false); }
/** * Opens a connection to the database * * @param connectionSource the {@link ConnectionSource} implementation substitution, * that will be used instead of one from {@link Sql2o} instance. * @return instance of the {@link Connection} class. */ public Connection open(ConnectionSource connectionSource) { return new Connection(this, connectionSource, false); }
/** * Opens a connection to the database * * @return instance of the {@link Connection} class. */ public Connection open() { return new Connection(this, false); }
/** * Opens a connection to the database * @return instance of the {@link org.sql2o.Connection} class. */ public Connection open() { return new Connection(this, false); }
/** * Creates a {@link Query} * @param query the sql query string * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query){ Connection connection = new Connection(this, true); return connection.createQuery(query); }
/** * Creates a {@link Query} * @param query the sql query string * @param returnGeneratedKeys boolean value indicating if the database should return any generated keys. * @return the {@link Query} instance * * @deprecated create queries with {@link org.sql2o.Connection} class instead, using try-with-resource blocks * <code> * try (Connection con = sql2o.open()) { * return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); * } * </code> */ @Deprecated public Query createQuery(String query, boolean returnGeneratedKeys) { return new Connection(this, true).createQuery(query, returnGeneratedKeys); }
/** * 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; }