/** * If we created a transaction commit it. We have successfully processed all * the rows. */ protected void commitTransactionIfCreated() { if (createdTransaction) { transaction.commit(); logger.info("Committed transaction"); } }
/** * Execute the query returning the resulting List. */ @Override public List<T> call() throws Exception { try { return server.findList(query, transaction); } finally { // cleanup the underlying connection transaction.end(); } }
@Override public Object nextId(Transaction t) { boolean useTxnConnection = t != null; Connection c = null; PreparedStatement pstmt = null; ResultSet rset = null; try { c = useTxnConnection ? t.getConnection() : dataSource.getConnection(); pstmt = c.prepareStatement(sql); rset = pstmt.executeQuery(); if (rset.next()) { return rset.getInt(1); } else { String m = "Always expecting 1 row from " + sql; throw new PersistenceException(m); } } catch (SQLException e) { throw new PersistenceException("Error getting sequence nextval", e); } finally { if (useTxnConnection) { closeResources(rset, pstmt, null); } else { closeResources(rset, pstmt, c); } } }
/** * Create a transaction if one is not already active and set its batch mode * and batch size. */ protected void initTransactionIfRequired() { transaction = server.currentTransaction(); if (transaction == null || !transaction.isActive()) { transaction = server.beginTransaction(); createdTransaction = true; if (persistBatchSize > 1) { logger.info("Creating transaction, batchSize[" + persistBatchSize + "]"); transaction.setBatchMode(true); transaction.setBatchSize(persistBatchSize); transaction.setBatchGetGeneratedKeys(false); } else { // explicitly turn off JDBC batching in case // is has been turned on globally transaction.setBatchMode(false); logger.info("Creating transaction with no JDBC batching"); } } }
BeanDescriptor<ScriptInfo> beanDescriptor = server.getBeanDescriptor(ScriptInfo.class); Transaction transaction = server.createTransaction(TxIsolation.READ_COMMITED); try (Connection connection = transaction.getConnection()) { String tableName = beanDescriptor.getBaseTable(); ResultSet rs = connection.getMetaData() .findEach(bean -> resources.add(new MigrationResource(bean))); transaction.commit(); } catch (Exception e) { transaction.rollback(); } finally { transaction.end();
/** * <p>executeTx.</p> * * @param c a {@link ameba.db.ebean.support.ModelResourceStructure.TxCallable} object. * @param errorHandler a {@link ameba.db.ebean.support.ModelResourceStructure.TxCallable} object. * @param <O> a O object. * @return a O object. * @throws java.lang.Exception if any. */ @SuppressWarnings("unchecked") protected <O> O executeTx(final TxCallable<O> c, final TxCallable<O> errorHandler) throws Exception { Transaction transaction = beginTransaction(); configureTransDefault(transaction); return processTransactionError(transaction, t -> { Object o = null; try { o = c.call(t); t.commit(); } catch (Throwable e) { t.rollback(e); throw e; } finally { t.end(); } return (O) o; }, errorHandler); }
/** * Rollback the transaction if we where not successful in processing all the * rows. */ protected void rollbackTransactionIfCreated(Throwable e) { if (createdTransaction) { transaction.rollback(e); logger.info("Rolled back transaction"); } }
@Override public void flush() { currentTransaction().flush(); }
@Override public void register(TransactionCallback transactionCallback) { Transaction transaction = currentTransaction(); if (transaction == null) { throw new PersistenceException("Not currently active transaction when trying to register transactionCallback"); } transaction.register(transactionCallback); }
@SuppressWarnings("unchecked") private <T> List<T> findList(Query<T> query, Transaction t, boolean findOne) { SpiOrmQueryRequest<T> request = createQueryRequest(Type.LIST, query, t); request.profileLocationAll(); request.resetBeanCacheAutoMode(findOne); Object result = request.getFromQueryCache(); if (result != null) { return (List<T>) result; } if ((t == null || !t.isSkipCache()) && request.getFromBeanCache()) { return request.getBeanCacheHits(); } if (request.isUseDocStore()) { return docStore().findList(request); } try { request.initTransIfRequired(); return request.findList(); } finally { request.endTransIfRequired(); } }
/** * <p>flushBatch.</p> */ protected void flushBatch() { Transaction t = server.currentTransaction(); if (t != null) t.flushBatch(); }
/** * <p>executeTx.</p> * * @param r a {@link ameba.db.ebean.support.ModelResourceStructure.TxRunnable} object. * @param errorHandler error handler * @throws java.lang.Exception if any. */ protected void executeTx(final TxRunnable r, final TxRunnable errorHandler) throws Exception { Transaction transaction = beginTransaction(); configureTransDefault(transaction); processTransactionError(transaction, t -> { try { r.run(t); t.commit(); } catch (Throwable e) { t.rollback(e); throw e; } finally { t.end(); } return null; }, errorHandler != null ? (TxCallable) t -> { errorHandler.run(t); return null; } : null); }
/** * Create a transaction if one is not already active and set its batch mode * and batch size. */ protected void initTransactionIfRequired() { transaction = server.currentTransaction(); if (transaction == null || !transaction.isActive()) { transaction = server.beginTransaction(); createdTransaction = true; if (persistBatchSize > 1) { logger.info("Creating transaction, batchSize[" + persistBatchSize + "]"); transaction.setBatchMode(true); transaction.setBatchSize(persistBatchSize); transaction.setBatchGetGeneratedKeys(false); } else { // explicitly turn off JDBC batching in case // is has been turned on globally transaction.setBatchMode(false); logger.info("Creating transaction with no JDBC batching"); } } }
/** * Rollback the current transaction. */ @Override public void rollbackTransaction() { currentTransaction().rollback(); }
/** * The batch will be flushing automatically but you can use this to explicitly * flush the batch if you like. * <p> * Flushing occurs automatically when: * </p> * <ul> * <li>the batch size is reached</li> * <li>A query is executed on the same transaction</li> * <li>UpdateSql or CallableSql are mixed with bean save and delete</li> * <li>Transaction commit occurs</li> * <li>A getter method is called on a batched bean</li> * </ul> */ public static void flush() { currentTransaction().flush(); }
@Override public void register(TransactionCallback transactionCallback) { Transaction transaction = currentTransaction(); if (transaction == null) { throw new PersistenceException("Not currently active transaction when trying to register transactionCallback"); } transaction.register(transactionCallback); }
@SuppressWarnings("unchecked") private <T> List<T> findList(Query<T> query, Transaction t, boolean findOne) { SpiOrmQueryRequest<T> request = createQueryRequest(Type.LIST, query, t); request.profileLocationAll(); request.resetBeanCacheAutoMode(findOne); Object result = request.getFromQueryCache(); if (result != null) { return (List<T>) result; } if ((t == null || !t.isSkipCache()) && request.getFromBeanCache()) { return request.getBeanCacheHits(); } if (request.isUseDocStore()) { return docStore().findList(request); } try { request.initTransIfRequired(); return request.findList(); } finally { request.endTransIfRequired(); } }
/** * Commit the current transaction. */ @Override public void commitTransaction() { currentTransaction().commit(); }
/** * Execute the query returning the row count. */ @Override public Integer call() throws Exception { try { return server.findCountWithCopy(query, transaction); } finally { // cleanup the underlying connection transaction.end(); } }
/** * Rollback the transaction if we where not successful in processing all the * rows. */ protected void rollbackTransactionIfCreated(Throwable e) { if (createdTransaction) { transaction.rollback(e); logger.info("Rolled back transaction"); } }