public Sql2o commit(){ return this.commit(true).sql2o; }
@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(); }
public Sql2o commit() { return this.commit(true).sql2o; }
public Sql2o commit(){ return this.commit(true).sql2o; }
/** * Commit connection */ public static void commit() { localConnection.get().commit(); }
/** * 原子提交 * * @param supplier * @param <T> */ public static <T> T atomic(Supplier<T> supplier) { T result = null; try (Connection connection = sql2o.beginTransaction()) { connectionThreadLocal.set(connection); result = supplier.get(); connection.commit(); } catch (Exception e) { log.error("Transaction rollback", e); } finally { connectionThreadLocal.remove(); return result; } }
public static void delete_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 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 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 boolean addCurrentResults(String domain, List<Result> results, Instant timestamp) { boolean success; try (Connection connection = DBConnections.getInstance().getConnection(true)) { success = InternalDBOperations.addCurrentResults(connection, domain, results, timestamp); connection.commit(); } return success; }
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); } }
public static boolean addPeriodicResults(List<ResultDomainPair> rdpList, Instant beginTime, Instant endTime) { boolean success; try (Connection connection = DBConnections.getInstance().getConnection(true)) { success = InternalDBOperations.addPeriodicResults(connection, rdpList, beginTime, endTime); if (success) { reloadHistoricalSnapshots(connection); } connection.commit(); } return success; }
@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; }
@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; }
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; }
/** * 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(); }
@Override public UUID createPost(String title, String content, List<String> categories) { try (Connection conn = sql2o.beginTransaction()) { UUID postUuid = uuidGenerator.generate(); conn.createQuery("insert into posts(post_uuid, title, content, publishing_date) VALUES (:post_uuid, :title, :content, :date)") .addParameter("post_uuid", postUuid) .addParameter("title", title) .addParameter("content", content) .addParameter("date", new Date()) .executeUpdate(); categories.forEach((category) -> conn.createQuery("insert into posts_categories(post_uuid, category) VALUES (:post_uuid, :category)") .addParameter("post_uuid", postUuid) .addParameter("category", category) .executeUpdate()); conn.commit(); return postUuid; } }
public Pizza save(Pizza pizza) { try (Connection con = sql2o.beginTransaction()) { long pizzaId = (long) con.createQuery("INSERT INTO pizza (base_id, name, price) VALUES (:baseId, :name, :price)", true) .addParameter("baseId", pizza.getBase().getId()) .addParameter("name", pizza.getName()) .addParameter("price", pizza.getPrice()) .executeUpdate() .getKey(); Query queryForPt = con.createQuery("INSERT INTO pizza_toppings (pizza_id, toppings_id) VALUES (:pizzaId, :toppingsId)"); pizza.getToppings().forEach(t -> queryForPt .addParameter("pizzaId", pizzaId) .addParameter("toppingsId", t.getId()) .addToBatch()); queryForPt.executeBatch(); con.commit(); } return pizza; }