#991 ENH: Add forUpdateNoWait() and forUpdateSkipLocked() options ... #528 ENH: Improve exception handling

This commit is contained in:
Rob Bygrave committed 2017-03-13 20:50:22 +13:00
1 parent a3dbd4538f
commit c8d01828b3
49 files changed
+985 -336

No files matched your search

@@ -5,7 +5,6 @@ import io.ebeaninternal.server.core.PersistRequest;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -123,7 +122,7 @@ public final class BatchControl {
* to the depth.
* </p>
*/
public int executeStatementOrBatch(PersistRequest request, boolean batch) {
public int executeStatementOrBatch(PersistRequest request, boolean batch) throws BatchedSqlException {
if (!batch || (batchFlushOnMixed && !isBeansEmpty())) {
// flush when mixing beans and updateSql
flush();
@@ -148,7 +147,7 @@ public final class BatchControl {
* immediately or queue it for batch processing later. The queue is flushedIntercept
* according to the depth (object graph depth).
*/
public int executeOrQueue(PersistRequestBean<?> request, boolean batch) {
public int executeOrQueue(PersistRequestBean<?> request, boolean batch) throws BatchedSqlException {
if (!batch || (batchFlushOnMixed && !pstmtHolder.isEmpty())) {
// flush when mixing beans and updateSql
@@ -167,7 +166,7 @@ public final class BatchControl {
/**
* Add the request to the batch and return true if we should flush.
*/
private boolean addToBatch(PersistRequestBean<?> request) {
private boolean addToBatch(PersistRequestBean<?> request) throws BatchedSqlException {
BatchedBeanHolder beanHolder = getBeanHolder(request);
int bufferSize = beanHolder.append(request);
@@ -193,14 +192,14 @@ public final class BatchControl {
/**
* Flush any batched PreparedStatements.
*/
protected void flushPstmtHolder() {
protected void flushPstmtHolder() throws BatchedSqlException {
pstmtHolder.flush(getGeneratedKeys);
}
/**
* Execute all the requests contained in the list.
*/
protected void executeNow(ArrayList<PersistRequest> list) {
protected void executeNow(ArrayList<PersistRequest> list) throws BatchedSqlException {
for (int i = 0; i < list.size(); i++) {
if (i % batchSize == 0) {
// hit the batch size so flush
@@ -214,14 +213,14 @@ public final class BatchControl {
/**
* Flush without resetting the topOrder (maintains the depth info).
*/
public void flush() throws PersistenceException {
public void flush() throws BatchedSqlException {
flush(false);
}
/**
* Flush with a reset the topOrder (fully empty the batch).
*/
public void flushReset() throws PersistenceException {
public void flushReset() throws BatchedSqlException {
flush(true);
}
@@ -236,31 +235,38 @@ public final class BatchControl {
/**
* execute all the requests currently queued or batched.
*/
private void flush(boolean resetTop) throws PersistenceException {
private void flush(boolean resetTop) throws BatchedSqlException {
if (!pstmtHolder.isEmpty()) {
// Flush existing pstmts (updateSql or callableSql)
flushPstmtHolder();
}
if (isEmpty()) {
// Nothing in queue to flush
return;
}
try {
if (!pstmtHolder.isEmpty()) {
// Flush existing pstmts (updateSql or callableSql)
flushPstmtHolder();
}
if (isEmpty()) {
// Nothing in queue to flush
return;
}
// convert entry map to array for sorting
BatchedBeanHolder[] bsArray = getBeanHolderArray();
// sort the entries by depth
Arrays.sort(bsArray, depthComparator);
// convert entry map to array for sorting
BatchedBeanHolder[] bsArray = getBeanHolderArray();
// sort the entries by depth
Arrays.sort(bsArray, depthComparator);
if (transaction.isLogSummary()) {
transaction.logSummary("BatchControl flush " + Arrays.toString(bsArray));
}
for (BatchedBeanHolder aBsArray : bsArray) {
aBsArray.executeNow();
}
if (transaction.isLogSummary()) {
transaction.logSummary("BatchControl flush " + Arrays.toString(bsArray));
}
for (BatchedBeanHolder aBsArray : bsArray) {
aBsArray.executeNow();
}
if (resetTop) {
beanHoldMap.clear();
if (resetTop) {
beanHoldMap.clear();
}
} catch (BatchedSqlException e) {
// clear the batch on error in case we want to
// catch, rollback and continue processing
clear();
throw e;
}
}
@@ -268,7 +274,7 @@ public final class BatchControl {
* Return an entry for the given type description. The type description is
* typically the bean class name (or table name for MapBeans).
*/
private BatchedBeanHolder getBeanHolder(PersistRequestBean<?> request) {
private BatchedBeanHolder getBeanHolder(PersistRequestBean<?> request) throws BatchedSqlException {
BeanDescriptor<?> beanDescriptor = request.getBeanDescriptor();
BatchedBeanHolder batchBeanHolder = beanHoldMap.get(beanDescriptor.getFullName());
@@ -79,7 +79,7 @@ public class BatchedBeanHolder {
* and then execute them.
* </p>
*/
public void executeNow() {
public void executeNow() throws BatchedSqlException {
// process the requests. Creates one or more PreparedStatements
// with binding addBatch() for each request.
// Note updates and deletes can result in many PreparedStatements
@@ -121,7 +121,7 @@ public class BatchedPstmt {
list.get(index).setGeneratedKey(idValue);
index++;
}
}
}
}
}
@@ -79,7 +79,7 @@ public class BatchedPstmtHolder {
*
* @param getGeneratedKeys if true try to get generated keys for inserts
*/
public void flush(boolean getGeneratedKeys) throws PersistenceException {
public void flush(boolean getGeneratedKeys) throws BatchedSqlException {
SQLException firstError = null;
String errorSql = null;
@@ -96,7 +96,7 @@ public class BatchedPstmtHolder {
} catch (SQLException ex) {
SQLException next = ex.getNextException();
while (next != null) {
logger.error("Next Exception during batch execution", next);
logger.trace("Next Exception during batch execution", next);
next = next.getNextException();
}
@@ -122,7 +122,7 @@ public class BatchedPstmtHolder {
if (firstError != null) {
String msg = "Error when batch flush on sql: " + errorSql;
throw new PersistenceException(msg, firstError);
throw new BatchedSqlException(msg, firstError);
}
}
@@ -0,0 +1,22 @@
package io.ebeaninternal.server.persist;
import java.sql.SQLException;
/**
* Holds the first SQLException found when executing a JDBC batch.
*/
public class BatchedSqlException extends Exception {
private SQLException cause;
BatchedSqlException(String message, SQLException cause) {
super(message, cause);
this.cause = cause;
}
@Override
public SQLException getCause() {
return cause;
}
}
@@ -1,10 +1,10 @@
package io.ebeaninternal.server.persist.dml;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.util.StringHelper;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.persist.BeanPersister;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
/**
@@ -18,15 +18,16 @@ import java.sql.SQLException;
*/
public final class DmlBeanPersister implements BeanPersister {
private final DatabasePlatform dbPlatform;
private final UpdateMeta updateMeta;
private final InsertMeta insertMeta;
private final DeleteMeta deleteMeta;
public DmlBeanPersister(UpdateMeta updateMeta, InsertMeta insertMeta, DeleteMeta deleteMeta) {
public DmlBeanPersister(DatabasePlatform dbPlatform, UpdateMeta updateMeta, InsertMeta insertMeta, DeleteMeta deleteMeta) {
this.dbPlatform = dbPlatform;
this.updateMeta = updateMeta;
this.insertMeta = insertMeta;
this.deleteMeta = deleteMeta;
@@ -37,9 +38,7 @@ public final class DmlBeanPersister implements BeanPersister {
*/
@Override
public int delete(PersistRequestBean<?> request) {
DeleteHandler delete = new DeleteHandler(request, deleteMeta);
return execute(request, delete);
return execute(request, new DeleteHandler(request, deleteMeta));
}
/**
@@ -47,9 +46,7 @@ public final class DmlBeanPersister implements BeanPersister {
*/
@Override
public void insert(PersistRequestBean<?> request) {
InsertHandler insert = new InsertHandler(request, insertMeta);
execute(request, insert);
execute(request, new InsertHandler(request, insertMeta));
}
/**
@@ -57,9 +54,7 @@ public final class DmlBeanPersister implements BeanPersister {
*/
@Override
public void update(PersistRequestBean<?> request) {
UpdateHandler update = new UpdateHandler(request, updateMeta);
execute(request, update);
execute(request, new UpdateHandler(request, updateMeta));
}
/**
@@ -80,12 +75,12 @@ public final class DmlBeanPersister implements BeanPersister {
} catch (SQLException e) {
// log the error to the transaction log
String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r", "\n"}, "\\n ");
String msg = "ERROR executing DML bindLog[" + handler.getBindLog() + "] error[" + errMsg + "]";
String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r", "\n"}, " ");
String msg = "Error[" + errMsg + "]";
if (request.getTransaction().isLogSummary()) {
request.getTransaction().logSummary(msg);
}
throw new PersistenceException(msg, e);
throw dbPlatform.translate(msg, e);
} finally {
if (!batched) {
@@ -10,9 +10,12 @@ import io.ebeaninternal.server.persist.BeanPersisterFactory;
*/
public class DmlBeanPersisterFactory implements BeanPersisterFactory {
private final DatabasePlatform dbPlatform;
private final MetaFactory metaFactory;
public DmlBeanPersisterFactory(DatabasePlatform dbPlatform) {
this.dbPlatform = dbPlatform;
this.metaFactory = new MetaFactory(dbPlatform);
}
@@ -29,7 +32,7 @@ public class DmlBeanPersisterFactory implements BeanPersisterFactory {
UpdateMeta updMeta = metaFactory.createUpdate(desc);
DeleteMeta delMeta = metaFactory.createDelete(desc);
InsertMeta insMeta = metaFactory.createInsert(desc);
return new DmlBeanPersister(updMeta, insMeta, delMeta);
return new DmlBeanPersister(dbPlatform, updMeta, insMeta, delMeta);
}
}