try (Connection con = sql2o.beginTransaction())
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. 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. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(){ return this.beginTransaction(java.sql.Connection.TRANSACTION_READ_COMMITTED); }
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. 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. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(ConnectionSource connectionSource) { return this.beginTransaction(connectionSource, java.sql.Connection.TRANSACTION_READ_COMMITTED); }
/** * 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 isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel){ return beginTransaction(getConnectionSource(), isolationLevel); }
@SuppressWarnings("unchecked") public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel){ Connection connection = this.beginTransaction(isolationLevel); Object result; try{ result = runnableWithResult.run(connection, argument); } catch (Throwable throwable) { connection.rollback(); throw new Sql2oException("An error occurred while executing StatementRunnableWithResult. Transaction rolled back.", throwable); } connection.commit(); return (V)result; }
/** * Calls the {@link StatementRunnable#run(Connection, Object)} method on the {@link StatementRunnable} parameter. All statements * run on the {@link Connection} instance in the {@link StatementRunnable#run(Connection, Object) run} method will be * executed in a transaction. The transaction will automatically be committed if the {@link StatementRunnable#run(Connection, Object) run} * method finishes without throwing an exception. If an exception is thrown within the {@link StatementRunnable#run(Connection, Object) run} method, * the transaction will automatically be rolled back. * @param runnable The {@link StatementRunnable} instance. * @param argument An argument which will be forwarded to the {@link StatementRunnable#run(Connection, Object) run} method * @param isolationLevel The isolation level of the transaction */ public void runInTransaction(StatementRunnable runnable, Object argument, int isolationLevel){ Connection connection = this.beginTransaction(isolationLevel); connection.setRollbackOnException(false); try { runnable.run(connection, argument); } catch (Throwable throwable) { connection.rollback(); throw new Sql2oException("An error occurred while executing StatementRunnable. Transaction is rolled back.", throwable); } connection.commit(); }
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. 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. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(ConnectionSource connectionSource) { return this.beginTransaction(connectionSource, java.sql.Connection.TRANSACTION_READ_COMMITTED); }
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. 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. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(){ return this.beginTransaction(java.sql.Connection.TRANSACTION_READ_COMMITTED); }
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. 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. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(ConnectionSource connectionSource) { return this.beginTransaction(connectionSource, java.sql.Connection.TRANSACTION_READ_COMMITTED); }
public Connection getConnection(boolean serialisable) { if (serialisable) { return database.beginTransaction(TRANSACTION_SERIALIZABLE); } else { return database.open(); } } }
/** * 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 isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel) { return beginTransaction(getConnectionSource(), isolationLevel); }
/** * 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 isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel){ return beginTransaction(getConnectionSource(), isolationLevel); }
public static <T> T find_by_id(int id ,Class<T> tClass){ String[] tablename =tClass.getName().toLowerCase().split("[.]"); String table = tablename[tablename.length-1]; List<T> list = (List<T>) DB.sql2o.beginTransaction().createQuery("select * from " + table+ " where id = " +id).executeAndFetch(tClass); return list.get(0); }
public static <T> List<T> find_all(Class<T> tClass){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); Connection connection = DB.sql2o.beginTransaction(); String table = Util.getclassName(tClass).toLowerCase(); String sql = "select * from "+table; logger.info(Constant.LOG_AC_NAME+"execute sql " + sql); List<T> list = connection.createQuery(sql).executeAndFetch(tClass); return list; }
public static void update_by_sql(String sql){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); logger.info(Constant.LOG_AC_NAME+" execute sql " + sql); DB.sql2o.beginTransaction().createQuery(sql).executeUpdate().commit(); }
public static Object insert_by_sql(String sql){ Logger logger = LogManager.getLogger(ActiveRecordBase.class); logger.info(Constant.LOG_AC_NAME+" execute sql " + sql); return DB.sql2o.beginTransaction().createQuery(sql).executeUpdate().getKey(); }
public static void delete(int id ,Class<?> c){ String name = Util.getclassName(c).toLowerCase(); String sql = "delete from "+ name + " where id = '" + id+"'"; Logger logger = LogManager.getLogger(ActiveRecordBase.class); logger.info(Constant.LOG_AC_NAME+" execute sql " + sql); DB.sql2o.beginTransaction().createQuery(sql).executeUpdate().commit(); } public static void delete_by_sql(String sql){
public static ActiveRecord where(String params , String value){ Connection connection = DB.sql2o.beginTransaction(); List<WhereParams> list = new ArrayList<WhereParams>(); WhereParams whereParams = new WhereParams(params,value,"="); list.add(whereParams); ActiveRecord activeRecord = new ActiveRecord(); activeRecord.setConnection(connection); CustomSql customSql = new CustomSql(); customSql.setWhere(list); activeRecord.setCustomSql(customSql); return activeRecord; }
public static ActiveRecord whereLt(String key ,String value ){ Connection connection = DB.sql2o.beginTransaction(); List<WhereParams> list = new ArrayList<WhereParams>(); WhereParams whereParams = new WhereParams(key,value,"<"); list.add(whereParams); ActiveRecord activeRecord = new ActiveRecord(); activeRecord.setConnection(connection); CustomSql customSql = new CustomSql(); customSql.setWhere(list); activeRecord.setCustomSql(customSql); return activeRecord; }
@SuppressWarnings("unchecked") public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel) { Connection connection = this.beginTransaction(isolationLevel); Object result; try { result = runnableWithResult.run(connection, argument); } catch (Throwable throwable) { connection.rollback(); throw new Sql2oException("An error occurred while executing StatementRunnableWithResult. Transaction rolled back.", throwable); } connection.commit(); return (V) result; }