mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#220 - Refactor Persist batch, add effectively add PersistBatch.INSERT (to ALL and NONE) and allow batching per request (save(), insert(), update(), delete())
This commit is contained in:
1 parent
bb6571f426
commit
6bb38f2674
31 files changed
+1731
-973
No files matched your search
@@ -2,14 +2,14 @@ package com.avaje.ebeaninternal.server.persist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
/**
|
||||
* Controls the batch ordering of persist requests.
|
||||
@@ -27,24 +27,25 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
public final class BatchControl {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(BatchControl.class);
|
||||
|
||||
/**
|
||||
* Used to sort queue entries by depth.
|
||||
*/
|
||||
private static final BatchDepthComparator depthComparator = new BatchDepthComparator();
|
||||
|
||||
/**
|
||||
* The associated transaction.
|
||||
*/
|
||||
private final SpiTransaction transaction;
|
||||
|
||||
/**
|
||||
* Controls batching of the PreparedStatements. This should be flushed after
|
||||
* each 'depth'.
|
||||
*/
|
||||
private final BatchedPstmtHolder pstmtHolder = new BatchedPstmtHolder();
|
||||
|
||||
/**
|
||||
* Map of the BatchedBeanHolder objects. They each have a depth and are later
|
||||
* sorted by their depth to get the execution order.
|
||||
*/
|
||||
private final HashMap<String, BatchedBeanHolder> beanHoldMap = new HashMap<String, BatchedBeanHolder>();
|
||||
|
||||
private final SpiTransaction transaction;
|
||||
|
||||
/**
|
||||
* The size at which the batch queue will flush. This should be close to the
|
||||
* number of statements that are batched into a single PreparedStatement. This
|
||||
@@ -60,17 +61,13 @@ public final class BatchControl {
|
||||
|
||||
private boolean batchFlushOnMixed = true;
|
||||
|
||||
private final BatchedBeanControl beanControl;
|
||||
|
||||
/**
|
||||
* Create for a given transaction, PersistExecute, default size and
|
||||
* getGeneratedKeys.
|
||||
* Create for a given transaction, PersistExecute, default size and getGeneratedKeys.
|
||||
*/
|
||||
public BatchControl(SpiTransaction t, int batchSize, boolean getGenKeys) {
|
||||
this.transaction = t;
|
||||
this.batchSize = batchSize;
|
||||
this.getGeneratedKeys = getGenKeys;
|
||||
this.beanControl = new BatchedBeanControl(t, this);
|
||||
transaction.setBatchControl(this);
|
||||
}
|
||||
|
||||
@@ -128,7 +125,7 @@ public final class BatchControl {
|
||||
* </p>
|
||||
*/
|
||||
public int executeStatementOrBatch(PersistRequest request, boolean batch) {
|
||||
if (!batch || (batchFlushOnMixed && !beanControl.isEmpty())) {
|
||||
if (!batch || (batchFlushOnMixed && !isBeansEmpty())) {
|
||||
// flush when mixing beans and updateSql
|
||||
flush();
|
||||
}
|
||||
@@ -161,31 +158,25 @@ public final class BatchControl {
|
||||
if (!batch) {
|
||||
return request.executeNow();
|
||||
}
|
||||
|
||||
// get the list we will add this request to
|
||||
ArrayList<PersistRequest> persistList = beanControl.getPersistList(request);
|
||||
if (persistList == null) {
|
||||
// special case where the same bean instance has been added
|
||||
// to the batch more than once
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Bean instance already in this batch: " + request.getEntityBean());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (persistList.size() >= batchSize) {
|
||||
// flush everything that has been batched
|
||||
if (addToBatch(request)) {
|
||||
// flush as the top level has hit the batch size
|
||||
flush();
|
||||
|
||||
// we need to get the persistList again after the
|
||||
// flush as the flush clears out the bean holders
|
||||
persistList = beanControl.getPersistList(request);
|
||||
}
|
||||
|
||||
persistList.add(request);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the request to the batch and return true if we should flush.
|
||||
*/
|
||||
private boolean addToBatch(PersistRequestBean<?> request) {
|
||||
|
||||
BatchedBeanHolder beanHolder = getBeanHolder(request);
|
||||
int bufferSize = beanHolder.append(request);
|
||||
|
||||
// return true if top level has hit batch size
|
||||
return bufferSize == batchSize && beanHolder.getOrder() == 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual batch of PreparedStatements.
|
||||
*/
|
||||
@@ -197,7 +188,7 @@ public final class BatchControl {
|
||||
* Return true if the queue is empty.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return (beanControl.isEmpty() && pstmtHolder.isEmpty());
|
||||
return (isBeansEmpty() && pstmtHolder.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,27 +203,45 @@ public final class BatchControl {
|
||||
*/
|
||||
protected void executeNow(ArrayList<PersistRequest> list) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (i % batchSize == 0) {
|
||||
// hit the batch size so flush
|
||||
flushPstmtHolder();
|
||||
}
|
||||
list.get(i).executeNow();
|
||||
}
|
||||
flushPstmtHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush without resetting the topOrder (maintains the depth info).
|
||||
*/
|
||||
public void flush() throws PersistenceException {
|
||||
flush(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush with a reset the topOrder (fully empty the batch).
|
||||
*/
|
||||
public void flushReset() throws PersistenceException {
|
||||
flush(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* execute all the requests currently queued or batched.
|
||||
*/
|
||||
public void flush() throws PersistenceException {
|
||||
private void flush(boolean resetTop) throws PersistenceException {
|
||||
|
||||
if (!pstmtHolder.isEmpty()) {
|
||||
// Flush existing pstmts (updateSql or callableSql)
|
||||
flushPstmtHolder();
|
||||
}
|
||||
if (beanControl.isEmpty()) {
|
||||
if (isEmpty()) {
|
||||
// Nothing in queue to flush
|
||||
return;
|
||||
}
|
||||
|
||||
// convert entry map to array for sorting
|
||||
BatchedBeanHolder[] bsArray = beanControl.getArray();
|
||||
|
||||
BatchedBeanHolder[] bsArray = getBeanHolderArray();
|
||||
// sort the entries by depth
|
||||
Arrays.sort(bsArray, depthComparator);
|
||||
|
||||
@@ -240,11 +249,55 @@ public final class BatchControl {
|
||||
transaction.logSummary("BatchControl flush " + Arrays.toString(bsArray));
|
||||
}
|
||||
for (int i = 0; i < bsArray.length; i++) {
|
||||
BatchedBeanHolder bs = bsArray[i];
|
||||
bs.executeNow();
|
||||
// flush all the batched Pstmts
|
||||
flushPstmtHolder();
|
||||
bsArray[i].executeNow();
|
||||
}
|
||||
|
||||
if (resetTop) {
|
||||
beanHoldMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
BeanDescriptor<?> beanDescriptor = request.getBeanDescriptor();
|
||||
BatchedBeanHolder batchBeanHolder = beanHoldMap.get(beanDescriptor.getFullName());
|
||||
if (batchBeanHolder == null) {
|
||||
int relativeDepth = transaction.depth();
|
||||
if (relativeDepth == 0 && !beanHoldMap.isEmpty()) {
|
||||
// flush and reset the batch as we are changing the type of our top level
|
||||
// bean so just keep it simple and flush and reset the top
|
||||
flushReset();
|
||||
}
|
||||
|
||||
batchBeanHolder = new BatchedBeanHolder(this, beanDescriptor, 100 + relativeDepth);
|
||||
beanHoldMap.put(beanDescriptor.getFullName(), batchBeanHolder);
|
||||
}
|
||||
return batchBeanHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this holds no persist requests.
|
||||
*/
|
||||
private boolean isBeansEmpty() {
|
||||
if (beanHoldMap.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (BatchedBeanHolder beanHolder : beanHoldMap.values()) {
|
||||
if (!beanHolder.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the BatchedBeanHolder's ready for sorting and executing.
|
||||
*/
|
||||
private BatchedBeanHolder[] getBeanHolderArray() {
|
||||
return beanHoldMap.values().toArray(new BatchedBeanHolder[beanHoldMap.size()]);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.persist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequest;
|
||||
import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
/**
|
||||
* Holds all the batched beans.
|
||||
* <p>
|
||||
* The beans are held here which delays the binding to a PreparedStatement. This
|
||||
* 'delayed' binding is required as the beans need to be bound and executed in
|
||||
* the correct order (according to the depth).
|
||||
* </p>
|
||||
*/
|
||||
public class BatchedBeanControl {
|
||||
|
||||
/**
|
||||
* Map of the BatchedBeanHolder objects. They each have a depth and are later
|
||||
* sorted by their depth to get the execution order.
|
||||
*/
|
||||
private final HashMap<String, BatchedBeanHolder> beanHoldMap = new HashMap<String, BatchedBeanHolder>();
|
||||
|
||||
private final SpiTransaction transaction;
|
||||
|
||||
private final BatchControl batchControl;
|
||||
|
||||
private int topOrder;
|
||||
|
||||
public BatchedBeanControl(SpiTransaction t, BatchControl batchControl) {
|
||||
this.transaction = t;
|
||||
this.batchControl = batchControl;
|
||||
}
|
||||
|
||||
public ArrayList<PersistRequest> getPersistList(PersistRequestBean<?> request) {
|
||||
return getBeanHolder(request).getList(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
BeanDescriptor<?> beanDescriptor = request.getBeanDescriptor();
|
||||
BatchedBeanHolder batchBeanHolder = beanHoldMap.get(beanDescriptor.getFullName());
|
||||
if (batchBeanHolder == null) {
|
||||
int relativeDepth = transaction.depth(0);
|
||||
if (relativeDepth == 0){
|
||||
topOrder++;
|
||||
}
|
||||
int stmtOrder = topOrder*100 + relativeDepth;
|
||||
|
||||
batchBeanHolder = new BatchedBeanHolder(batchControl, beanDescriptor, stmtOrder);
|
||||
beanHoldMap.put(beanDescriptor.getFullName(), batchBeanHolder);
|
||||
}
|
||||
return batchBeanHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this holds no persist requests.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return beanHoldMap.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the BatchedBeanHolder's ready for sorting and executing.
|
||||
*/
|
||||
public BatchedBeanHolder[] getArray() {
|
||||
BatchedBeanHolder[] bsArray = new BatchedBeanHolder[beanHoldMap.size()];
|
||||
beanHoldMap.values().toArray(bsArray);
|
||||
return bsArray;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,10 +8,10 @@ import com.avaje.ebeaninternal.server.core.PersistRequestBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
/**
|
||||
* Holds lists of persist requests for beans of a given typeDescription.
|
||||
* Holds lists of persist requests for beans of a given type.
|
||||
* <p>
|
||||
* This is used to delay the actual binding of the bean to PreparedStatements.
|
||||
* The reason is that don't have all the bind values yet in the case of inserts
|
||||
* The reason is that we don't have all the bind values yet in the case of inserts
|
||||
* with getGeneratedKeys.
|
||||
* </p>
|
||||
* <p>
|
||||
@@ -21,118 +21,139 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
*/
|
||||
public class BatchedBeanHolder {
|
||||
|
||||
/**
|
||||
* The owning queue.
|
||||
*/
|
||||
private final BatchControl control;
|
||||
/**
|
||||
* The owning queue.
|
||||
*/
|
||||
private final BatchControl control;
|
||||
|
||||
private final String shortDesc;
|
||||
private final String shortDesc;
|
||||
|
||||
/**
|
||||
* The 'depth' which is used to determine the execution order.
|
||||
*/
|
||||
private final int order;
|
||||
/**
|
||||
* The 'depth' which is used to determine the execution order.
|
||||
*/
|
||||
private final int order;
|
||||
|
||||
/**
|
||||
* The list of bean insert requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> inserts;
|
||||
/**
|
||||
* The list of bean insert requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> inserts;
|
||||
|
||||
/**
|
||||
* The list of bean update requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> updates;
|
||||
/**
|
||||
* The list of bean update requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> updates;
|
||||
|
||||
/**
|
||||
* The list of bean delete requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> deletes;
|
||||
/**
|
||||
* The list of bean delete requests.
|
||||
*/
|
||||
private ArrayList<PersistRequest> deletes;
|
||||
|
||||
private HashSet<Integer> beanHashCodes = new HashSet<Integer>();
|
||||
|
||||
/**
|
||||
* Create a new entry with a given type and depth.
|
||||
*/
|
||||
public BatchedBeanHolder(BatchControl control, BeanDescriptor<?> beanDescriptor, int order) {
|
||||
this.control = control;
|
||||
this.shortDesc = beanDescriptor.getName() + ":" + order;
|
||||
this.order = order;
|
||||
}
|
||||
private HashSet<Integer> beanHashCodes = new HashSet<Integer>();
|
||||
|
||||
/**
|
||||
* Return the depth.
|
||||
*/
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute all the persist requests in this entry.
|
||||
* <p>
|
||||
* This will Batch all the similar requests into one or more BatchStatements
|
||||
* and then execute them.
|
||||
* </p>
|
||||
*/
|
||||
public void executeNow() {
|
||||
// process the requests. Creates one or more PreparedStatements
|
||||
// with binding addBatch() for each request.
|
||||
/**
|
||||
* Create a new entry with a given type and depth.
|
||||
*/
|
||||
public BatchedBeanHolder(BatchControl control, BeanDescriptor<?> beanDescriptor, int order) {
|
||||
this.control = control;
|
||||
this.shortDesc = beanDescriptor.getName() + ":" + order;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
// Note updates and deletes can result in many PreparedStatements
|
||||
// if their where clauses differ via use of IS NOT NULL.
|
||||
if (inserts != null && !inserts.isEmpty()) {
|
||||
control.executeNow(inserts);
|
||||
inserts.clear();
|
||||
}
|
||||
if (updates != null && !updates.isEmpty()) {
|
||||
control.executeNow(updates);
|
||||
updates.clear();
|
||||
}
|
||||
if (deletes != null && !deletes.isEmpty()) {
|
||||
control.executeNow(deletes);
|
||||
deletes.clear();
|
||||
}
|
||||
beanHashCodes.clear();
|
||||
}
|
||||
/**
|
||||
* Return the depth.
|
||||
*/
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return shortDesc;
|
||||
}
|
||||
/**
|
||||
* Execute all the persist requests in this entry.
|
||||
* <p>
|
||||
* This will Batch all the similar requests into one or more BatchStatements
|
||||
* and then execute them.
|
||||
* </p>
|
||||
*/
|
||||
public void executeNow() {
|
||||
// process the requests. Creates one or more PreparedStatements
|
||||
// with binding addBatch() for each request.
|
||||
// Note updates and deletes can result in many PreparedStatements
|
||||
// if their where clauses differ via use of IS NOT NULL.
|
||||
if (inserts != null && !inserts.isEmpty()) {
|
||||
control.executeNow(inserts);
|
||||
inserts.clear();
|
||||
}
|
||||
if (updates != null && !updates.isEmpty()) {
|
||||
control.executeNow(updates);
|
||||
updates.clear();
|
||||
}
|
||||
if (deletes != null && !deletes.isEmpty()) {
|
||||
control.executeNow(deletes);
|
||||
deletes.clear();
|
||||
}
|
||||
beanHashCodes.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list for the typeCode.
|
||||
*/
|
||||
public ArrayList<PersistRequest> getList(PersistRequestBean<?> request) {
|
||||
|
||||
Integer objHashCode = Integer.valueOf(System.identityHashCode(request.getEntityBean()));
|
||||
|
||||
if (!beanHashCodes.add(objHashCode)) {
|
||||
// special case where the same bean instance has already been
|
||||
// added to the batch (doesn't really occur with non-batching
|
||||
// as the bean gets changed from dirty to loaded earlier)
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (request.getType()) {
|
||||
case INSERT:
|
||||
if (inserts == null) {
|
||||
inserts = new ArrayList<PersistRequest>();
|
||||
}
|
||||
return inserts;
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(shortDesc.length()+18);
|
||||
sb.append(shortDesc);
|
||||
if (inserts != null) {
|
||||
sb.append(" i:").append(inserts.size());
|
||||
}
|
||||
if (updates != null) {
|
||||
sb.append(" u:").append(updates.size());
|
||||
}
|
||||
if (deletes != null) {
|
||||
sb.append(" d:").append(deletes.size());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
case UPDATE:
|
||||
if (updates == null) {
|
||||
updates = new ArrayList<PersistRequest>();
|
||||
}
|
||||
return updates;
|
||||
/**
|
||||
* Add the request to the appropriate persist list.
|
||||
*/
|
||||
public int append(PersistRequestBean<?> request) {
|
||||
|
||||
case DELETE:
|
||||
if (deletes == null) {
|
||||
deletes = new ArrayList<PersistRequest>();
|
||||
}
|
||||
return deletes;
|
||||
Integer objHashCode = new Integer(System.identityHashCode(request.getEntityBean()));
|
||||
if (!beanHashCodes.add(objHashCode)) {
|
||||
// special case where the same bean instance has already been
|
||||
// added to the batch (doesn't really occur with non-batching
|
||||
// as the bean gets changed from dirty to loaded earlier)
|
||||
return 0;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid type code " + request.getType());
|
||||
}
|
||||
}
|
||||
request.setBatched();
|
||||
|
||||
switch (request.getType()) {
|
||||
case INSERT:
|
||||
if (inserts == null) {
|
||||
inserts = new ArrayList<PersistRequest>();
|
||||
}
|
||||
inserts.add(request);
|
||||
return inserts.size();
|
||||
|
||||
case UPDATE:
|
||||
if (updates == null) {
|
||||
updates = new ArrayList<PersistRequest>();
|
||||
}
|
||||
updates.add(request);
|
||||
return updates.size();
|
||||
|
||||
case DELETE:
|
||||
if (deletes == null) {
|
||||
deletes = new ArrayList<PersistRequest>();
|
||||
}
|
||||
deletes.add(request);
|
||||
return deletes.size();
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid type code " + request.getType());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is empty containing no batched beans.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return beanHashCodes.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -168,20 +168,19 @@ public final class DefaultPersister implements Persister {
|
||||
PersistRequestBean<?> req = createRequest(entityBean, t, null, PersistRequest.Type.UPDATE);
|
||||
req.setDeleteMissingChildren(deleteMissingChildren);
|
||||
try {
|
||||
req.initTransIfRequired();
|
||||
|
||||
req.initTransIfRequiredWithBatchCascade();
|
||||
if (req.isReference()) {
|
||||
// its a reference so see if there are manys to save...
|
||||
if (req.isPersistCascade()) {
|
||||
saveAssocMany(false, req, false);
|
||||
}
|
||||
req.checkUpdatedManysOnly();
|
||||
|
||||
} else {
|
||||
update(req);
|
||||
}
|
||||
|
||||
req.commitTransIfRequired();
|
||||
req.flushBatchOnCascade();
|
||||
|
||||
} catch (RuntimeException ex) {
|
||||
req.rollbackTransIfRequired();
|
||||
@@ -208,9 +207,10 @@ public final class DefaultPersister implements Persister {
|
||||
|
||||
PersistRequestBean<?> req = createRequest(bean, t, null, PersistRequest.Type.INSERT);
|
||||
try {
|
||||
req.initTransIfRequired();
|
||||
req.initTransIfRequiredWithBatchCascade();
|
||||
insert(req);
|
||||
req.commitTransIfRequired();
|
||||
req.flushBatchOnCascade();
|
||||
|
||||
} catch (RuntimeException ex) {
|
||||
req.rollbackTransIfRequired();
|
||||
@@ -323,9 +323,10 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
|
||||
try {
|
||||
req.initTransIfRequired();
|
||||
req.initTransIfRequiredWithBatchCascade();
|
||||
delete(req);
|
||||
req.commitTransIfRequired();
|
||||
req.flushBatchOnCascade();
|
||||
|
||||
} catch (RuntimeException ex) {
|
||||
req.rollbackTransIfRequired();
|
||||
@@ -558,9 +559,7 @@ public final class DefaultPersister implements Persister {
|
||||
if (request.isLoadedProperty(prop)) {
|
||||
EntityBean detailBean = prop.getValueAsEntityBean(parentBean);
|
||||
if (detailBean != null) {
|
||||
if (prop.isSaveRecurseSkippable(detailBean)) {
|
||||
// skip saving this bean
|
||||
} else {
|
||||
if (!prop.isSaveRecurseSkippable(detailBean)) {
|
||||
t.depth(+1);
|
||||
prop.setParentBeanToChild(parentBean, detailBean);
|
||||
saveRecurse(detailBean, t, parentBean, insertMode);
|
||||
@@ -748,11 +747,11 @@ public final class DefaultPersister implements Persister {
|
||||
// set it to the appropriate property on the
|
||||
// detail bean before we save it
|
||||
boolean isMap = ManyType.JAVA_MAP.equals(prop.getManyType());
|
||||
EntityBean parentBean = (EntityBean)saveMany.getParentBean();
|
||||
EntityBean parentBean = saveMany.getParentBean();
|
||||
Object mapKeyValue = null;
|
||||
|
||||
boolean saveSkippable = prop.isSaveRecurseSkippable();
|
||||
boolean skipSavingThisBean = false;
|
||||
boolean skipSavingThisBean;
|
||||
|
||||
for (Object detailBean : collection) {
|
||||
if (isMap) {
|
||||
@@ -762,11 +761,7 @@ public final class DefaultPersister implements Persister {
|
||||
detailBean = entry.getValue();
|
||||
}
|
||||
|
||||
if (detailBean instanceof EntityBean == false) {
|
||||
skipSavingThisBean = true;
|
||||
logger.debug("Skip non entity bean");
|
||||
|
||||
} else {
|
||||
if (detailBean instanceof EntityBean) {
|
||||
EntityBean detail = (EntityBean)detailBean;
|
||||
EntityBeanIntercept ebi = detail._ebean_getIntercept();
|
||||
if (prop.isManyToMany()) {
|
||||
@@ -787,14 +782,7 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
}
|
||||
|
||||
if (skipSavingThisBean) {
|
||||
// unmodified bean that does not recurse its save
|
||||
// so we can skip the save for this bean.
|
||||
// Reset skipSavingThisBean for the next detailBean
|
||||
skipSavingThisBean = false;
|
||||
|
||||
} else {
|
||||
// normal save recurse
|
||||
if (!skipSavingThisBean) {
|
||||
saveRecurse(detail, t, parentBean, insertMode);
|
||||
}
|
||||
if (detailIds != null) {
|
||||
@@ -879,9 +867,6 @@ public final class DefaultPersister implements Persister {
|
||||
}
|
||||
|
||||
SpiTransaction t = saveManyPropRequest.getTransaction();
|
||||
Collection<?> additions = null;
|
||||
Collection<?> deletions = null;
|
||||
|
||||
boolean vanillaCollection = !(value instanceof BeanCollection<?>);
|
||||
|
||||
if (vanillaCollection || deleteMissingChildren) {
|
||||
@@ -890,6 +875,9 @@ public final class DefaultPersister implements Persister {
|
||||
deleteAssocManyIntersection(saveManyPropRequest.getParentBean(), prop, t);
|
||||
}
|
||||
|
||||
Collection<?> deletions = null;
|
||||
Collection<?> additions;
|
||||
|
||||
if (saveManyPropRequest.isInsertedParent() || vanillaCollection || deleteMissingChildren) {
|
||||
// treat everything in the list/set/map as an intersection addition
|
||||
if (value instanceof Map<?, ?>) {
|
||||
@@ -1098,30 +1086,24 @@ public final class DefaultPersister implements Persister {
|
||||
// imported ones with save cascade
|
||||
BeanPropertyAssocOne<?>[] ones = desc.propertiesOneImportedSave();
|
||||
|
||||
for (int i = 0; i < ones.length; i++) {
|
||||
BeanPropertyAssocOne<?> prop = ones[i];
|
||||
for (int i = 0; i < ones.length; i++) {
|
||||
BeanPropertyAssocOne<?> prop = ones[i];
|
||||
|
||||
// check for partial objects
|
||||
if (request.isLoadedProperty(prop)) {
|
||||
EntityBean detailBean = prop.getValueAsEntityBean(request.getEntityBean());
|
||||
if (detailBean != null) {
|
||||
if (prop.isReference(detailBean)) {
|
||||
// skip saving a reference
|
||||
} else if (request.isParent(detailBean)) {
|
||||
// skip saving the parent as already saved
|
||||
} else if (prop.isSaveRecurseSkippable(detailBean)) {
|
||||
// we can skip saving this bean
|
||||
|
||||
} else {
|
||||
SpiTransaction t = request.getTransaction();
|
||||
t.depth(-1);
|
||||
saveRecurse(detailBean, t, null, insertMode);
|
||||
t.depth(+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// check for partial objects
|
||||
if (request.isLoadedProperty(prop)) {
|
||||
EntityBean detailBean = prop.getValueAsEntityBean(request.getEntityBean());
|
||||
if (detailBean != null
|
||||
&& !prop.isSaveRecurseSkippable(detailBean)
|
||||
&& !prop.isReference(detailBean)
|
||||
&& !request.isParent(detailBean)) {
|
||||
SpiTransaction t = request.getTransaction();
|
||||
t.depth(-1);
|
||||
saveRecurse(detailBean, t, null, insertMode);
|
||||
t.depth(+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Support for loading any Imported Associated One properties that are not
|
||||
@@ -1156,10 +1138,7 @@ public final class DefaultPersister implements Persister {
|
||||
|
||||
for (int i = 0; i < ones.length; i++) {
|
||||
BeanPropertyAssocOne<?> prop = ones[i];
|
||||
if (!request.isLoadedProperty(prop)) {
|
||||
// handled by DeleteUnloadedForeignKeys that was built
|
||||
// via getDeleteUnloadedForeignKeys();
|
||||
} else {
|
||||
if (request.isLoadedProperty(prop)) {
|
||||
Object detailBean = prop.getValue(request.getEntityBean());
|
||||
if (detailBean != null) {
|
||||
EntityBean detail = (EntityBean)detailBean;
|
||||
|
||||
@@ -20,100 +20,92 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
public class ExeCallableSql {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeCallableSql.class);
|
||||
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
public ExeCallableSql(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.binder = binder;
|
||||
// no batch support for CallableStatement in Oracle anyway
|
||||
this.pstmtFactory = new PstmtFactory(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* execute the CallableSql requests.
|
||||
*/
|
||||
public int execute(PersistRequestCallableSql request) {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeCallableSql.class);
|
||||
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
boolean batchThisRequest = t.isBatchThisRequest();
|
||||
|
||||
CallableStatement cstmt = null;
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
public ExeCallableSql(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.binder = binder;
|
||||
// no batch support for CallableStatement in Oracle anyway
|
||||
this.pstmtFactory = new PstmtFactory(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* execute the CallableSql requests.
|
||||
*/
|
||||
public int execute(PersistRequestCallableSql request) {
|
||||
|
||||
boolean batchThisRequest = request.isBatchThisRequest();
|
||||
|
||||
CallableStatement cstmt = null;
|
||||
try {
|
||||
cstmt = bindStmt(request, batchThisRequest);
|
||||
if (batchThisRequest) {
|
||||
cstmt.addBatch();
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
} else {
|
||||
// handles executeOverride() and also
|
||||
// reading of registered OUT parameters
|
||||
int rowCount = request.executeUpdate();
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new PersistenceException(ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && cstmt != null) {
|
||||
try {
|
||||
|
||||
cstmt = bindStmt(request, batchThisRequest);
|
||||
|
||||
if (batchThisRequest){
|
||||
cstmt.addBatch();
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
|
||||
} else {
|
||||
// handles executeOverride() and also
|
||||
// reading of registered OUT parameters
|
||||
int rowCount = request.executeUpdate();
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new PersistenceException(ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && cstmt != null) {
|
||||
try {
|
||||
cstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
cstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private CallableStatement bindStmt(PersistRequestCallableSql request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiCallableSql callableSql = request.getCallableSql();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = callableSql.getSql();
|
||||
|
||||
BindParams bindParams = callableSql.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
CallableStatement cstmt;
|
||||
if (batchThisRequest){
|
||||
cstmt = pstmtFactory.getCstmt(t, logSql, sql, request);
|
||||
|
||||
} else {
|
||||
if (logSql){
|
||||
t.logSql(sql);
|
||||
}
|
||||
cstmt = pstmtFactory.getCstmt(t, sql);
|
||||
}
|
||||
|
||||
if (callableSql.getTimeout() > 0){
|
||||
cstmt.setQueryTimeout(callableSql.getTimeout());
|
||||
}
|
||||
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()){
|
||||
bindLog = binder.bind(bindParams, new DataBind(cstmt));
|
||||
}
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
|
||||
// required to read OUT params later
|
||||
request.setBound(bindParams, cstmt);
|
||||
|
||||
return cstmt;
|
||||
}
|
||||
|
||||
|
||||
private CallableStatement bindStmt(PersistRequestCallableSql request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiCallableSql callableSql = request.getCallableSql();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = callableSql.getSql();
|
||||
|
||||
BindParams bindParams = callableSql.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
CallableStatement cstmt;
|
||||
if (batchThisRequest) {
|
||||
cstmt = pstmtFactory.getCstmt(t, logSql, sql, request);
|
||||
} else {
|
||||
if (logSql) {
|
||||
t.logSql(sql);
|
||||
}
|
||||
cstmt = pstmtFactory.getCstmt(t, sql);
|
||||
}
|
||||
|
||||
if (callableSql.getTimeout() > 0) {
|
||||
cstmt.setQueryTimeout(callableSql.getTimeout());
|
||||
}
|
||||
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()) {
|
||||
bindLog = binder.bind(bindParams, new DataBind(cstmt));
|
||||
}
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
|
||||
// required to read OUT params later
|
||||
request.setBound(bindParams, cstmt);
|
||||
return cstmt;
|
||||
}
|
||||
}
|
||||
@@ -21,121 +21,109 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
public class ExeOrmUpdate {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeOrmUpdate.class);
|
||||
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
/**
|
||||
* Create with a given binder.
|
||||
*/
|
||||
public ExeOrmUpdate(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.pstmtFactory = new PstmtFactory(pstmtBatch);
|
||||
this.binder = binder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the orm update request.
|
||||
*/
|
||||
public int execute(PersistRequestOrmUpdate request) {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeOrmUpdate.class);
|
||||
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
boolean batchThisRequest = t.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
/**
|
||||
* Create with a given binder.
|
||||
*/
|
||||
public ExeOrmUpdate(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.pstmtFactory = new PstmtFactory(pstmtBatch);
|
||||
this.binder = binder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the orm update request.
|
||||
*/
|
||||
public int execute(PersistRequestOrmUpdate request) {
|
||||
|
||||
boolean batchThisRequest = request.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
pstmt = bindStmt(request, batchThisRequest);
|
||||
if (batchThisRequest) {
|
||||
PstmtBatch pstmtBatch = request.getPstmtBatch();
|
||||
if (pstmtBatch != null) {
|
||||
pstmtBatch.addBatch(pstmt);
|
||||
} else {
|
||||
pstmt.addBatch();
|
||||
}
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
} else {
|
||||
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
|
||||
if (ormUpdate.getTimeout() > 0) {
|
||||
pstmt.setQueryTimeout(ormUpdate.getTimeout());
|
||||
}
|
||||
int rowCount = pstmt.executeUpdate();
|
||||
request.checkRowCount(rowCount);
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new PersistenceException("Error executing: " + request.getOrmUpdate().getGeneratedSql(), ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && pstmt != null) {
|
||||
try {
|
||||
|
||||
pstmt = bindStmt(request, batchThisRequest);
|
||||
|
||||
if (batchThisRequest){
|
||||
PstmtBatch pstmtBatch = request.getPstmtBatch();
|
||||
if (pstmtBatch != null){
|
||||
pstmtBatch.addBatch(pstmt);
|
||||
} else {
|
||||
pstmt.addBatch();
|
||||
}
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
|
||||
} else {
|
||||
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
|
||||
if (ormUpdate.getTimeout() > 0){
|
||||
pstmt.setQueryTimeout(ormUpdate.getTimeout());
|
||||
}
|
||||
|
||||
int rowCount = pstmt.executeUpdate();
|
||||
request.checkRowCount(rowCount);
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
|
||||
String msg = "Error executing: "+ormUpdate.getGeneratedSql();
|
||||
throw new PersistenceException(msg, ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && pstmt != null) {
|
||||
try {
|
||||
pstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
pstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bean and property names to db table and columns.
|
||||
*/
|
||||
private String translate(PersistRequestOrmUpdate request, String sql) {
|
||||
|
||||
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
|
||||
return descriptor.convertOrmUpdateToSql(sql);
|
||||
}
|
||||
|
||||
private PreparedStatement bindStmt(PersistRequestOrmUpdate request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = ormUpdate.getUpdateStatement();
|
||||
|
||||
// convert bean and property names to table and
|
||||
// column names if required
|
||||
sql = translate(request, sql);
|
||||
|
||||
BindParams bindParams = ormUpdate.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
|
||||
ormUpdate.setGeneratedSql(sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (batchThisRequest){
|
||||
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
|
||||
|
||||
} else {
|
||||
if (logSql){
|
||||
t.logSql(sql);
|
||||
}
|
||||
pstmt = pstmtFactory.getPstmt(t, sql);
|
||||
}
|
||||
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()){
|
||||
bindLog = binder.bind(bindParams, new DataBind(pstmt));
|
||||
}
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
|
||||
return pstmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bean and property names to db table and columns.
|
||||
*/
|
||||
private String translate(PersistRequestOrmUpdate request, String sql) {
|
||||
|
||||
BeanDescriptor<?> descriptor = request.getBeanDescriptor();
|
||||
return descriptor.convertOrmUpdateToSql(sql);
|
||||
}
|
||||
|
||||
private PreparedStatement bindStmt(PersistRequestOrmUpdate request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiUpdate<?> ormUpdate = request.getOrmUpdate();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = ormUpdate.getUpdateStatement();
|
||||
|
||||
// convert bean and property names to table and
|
||||
// column names if required
|
||||
sql = translate(request, sql);
|
||||
|
||||
BindParams bindParams = ormUpdate.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
|
||||
ormUpdate.setGeneratedSql(sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (batchThisRequest) {
|
||||
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
|
||||
} else {
|
||||
if (logSql) {
|
||||
t.logSql(sql);
|
||||
}
|
||||
pstmt = pstmtFactory.getPstmt(t, sql);
|
||||
}
|
||||
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()) {
|
||||
bindLog = binder.bind(bindParams, new DataBind(pstmt));
|
||||
}
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
return pstmt;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.persist;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import com.avaje.ebeaninternal.api.SpiSqlUpdate;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
@@ -16,185 +11,180 @@ import com.avaje.ebeaninternal.server.util.BindParamsParser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Executes the UpdateSql requests.
|
||||
*/
|
||||
public class ExeUpdateSql {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeUpdateSql.class);
|
||||
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
private final PstmtBatch pstmtBatch;
|
||||
|
||||
//TODO: get defaultBatchSize
|
||||
private int defaultBatchSize = 20;
|
||||
|
||||
/**
|
||||
* Create with a given binder.
|
||||
*/
|
||||
public ExeUpdateSql(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.binder = binder;
|
||||
this.pstmtBatch = pstmtBatch;
|
||||
this.pstmtFactory = new PstmtFactory(pstmtBatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the UpdateSql request.
|
||||
*/
|
||||
public int execute(PersistRequestUpdateSql request) {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ExeUpdateSql.class);
|
||||
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
boolean batchThisRequest = t.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
private final Binder binder;
|
||||
|
||||
private final PstmtFactory pstmtFactory;
|
||||
|
||||
private final PstmtBatch pstmtBatch;
|
||||
|
||||
private int defaultBatchSize = 20;
|
||||
|
||||
/**
|
||||
* Create with a given binder.
|
||||
*/
|
||||
public ExeUpdateSql(Binder binder, PstmtBatch pstmtBatch) {
|
||||
this.binder = binder;
|
||||
this.pstmtBatch = pstmtBatch;
|
||||
this.pstmtFactory = new PstmtFactory(pstmtBatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the UpdateSql request.
|
||||
*/
|
||||
public int execute(PersistRequestUpdateSql request) {
|
||||
|
||||
boolean batchThisRequest = request.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
|
||||
pstmt = bindStmt(request, batchThisRequest);
|
||||
|
||||
if (batchThisRequest) {
|
||||
if (pstmtBatch != null) {
|
||||
pstmtBatch.addBatch(pstmt);
|
||||
} else {
|
||||
pstmt.addBatch();
|
||||
}
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
} else {
|
||||
int rowCount = pstmt.executeUpdate();
|
||||
request.checkRowCount(rowCount);
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new PersistenceException(ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && pstmt != null) {
|
||||
try {
|
||||
|
||||
pstmt = bindStmt(request, batchThisRequest);
|
||||
|
||||
if (batchThisRequest){
|
||||
if (pstmtBatch != null){
|
||||
pstmtBatch.addBatch(pstmt);
|
||||
} else {
|
||||
pstmt.addBatch();
|
||||
}
|
||||
// return -1 to indicate batch mode
|
||||
return -1;
|
||||
|
||||
} else {
|
||||
int rowCount = pstmt.executeUpdate();
|
||||
request.checkRowCount(rowCount);
|
||||
request.postExecute();
|
||||
return rowCount;
|
||||
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
throw new PersistenceException(ex);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && pstmt != null) {
|
||||
try {
|
||||
pstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
pstmt.close();
|
||||
} catch (SQLException e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private PreparedStatement bindStmt(PersistRequestUpdateSql request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiSqlUpdate updateSql = request.getUpdateSql();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = updateSql.getSql();
|
||||
|
||||
BindParams bindParams = updateSql.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
updateSql.setGeneratedSql(sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (batchThisRequest){
|
||||
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
|
||||
if (pstmtBatch != null){
|
||||
// oracle specific JDBC setting batch size ahead of time
|
||||
int batchSize = t.getBatchSize();
|
||||
if (batchSize < 1){
|
||||
batchSize = defaultBatchSize;
|
||||
}
|
||||
pstmtBatch.setBatchSize(pstmt, batchSize);
|
||||
}
|
||||
|
||||
} else {
|
||||
if (logSql){
|
||||
t.logSql(sql);
|
||||
}
|
||||
pstmt = pstmtFactory.getPstmt(t, sql);
|
||||
}
|
||||
|
||||
if (updateSql.getTimeout() > 0){
|
||||
pstmt.setQueryTimeout(updateSql.getTimeout());
|
||||
}
|
||||
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()){
|
||||
bindLog = binder.bind(bindParams, new DataBind(pstmt));
|
||||
}
|
||||
|
||||
private PreparedStatement bindStmt(PersistRequestUpdateSql request, boolean batchThisRequest) throws SQLException {
|
||||
|
||||
SpiSqlUpdate updateSql = request.getUpdateSql();
|
||||
SpiTransaction t = request.getTransaction();
|
||||
|
||||
String sql = updateSql.getSql();
|
||||
|
||||
BindParams bindParams = updateSql.getBindParams();
|
||||
|
||||
// process named parameters if required
|
||||
sql = BindParamsParser.parse(bindParams, sql);
|
||||
updateSql.setGeneratedSql(sql);
|
||||
|
||||
boolean logSql = request.isLogSql();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (batchThisRequest) {
|
||||
pstmt = pstmtFactory.getPstmt(t, logSql, sql, request);
|
||||
if (pstmtBatch != null) {
|
||||
// oracle specific JDBC setting batch size ahead of time
|
||||
int batchSize = t.getBatchSize();
|
||||
if (batchSize < 1) {
|
||||
batchSize = defaultBatchSize;
|
||||
}
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
|
||||
// derive the statement type (for TransactionEvent)
|
||||
parseUpdate(sql, request);
|
||||
|
||||
return pstmt;
|
||||
pstmtBatch.setBatchSize(pstmt, batchSize);
|
||||
}
|
||||
} else {
|
||||
if (logSql) {
|
||||
t.logSql(sql);
|
||||
}
|
||||
pstmt = pstmtFactory.getPstmt(t, sql);
|
||||
}
|
||||
|
||||
|
||||
private void determineType(String word1, String word2, String word3, PersistRequestUpdateSql request) {
|
||||
if (word1.equalsIgnoreCase("UPDATE")) {
|
||||
request.setType(SqlType.SQL_UPDATE, word2, "UpdateSql");
|
||||
|
||||
} else if (word1.equalsIgnoreCase("DELETE")) {
|
||||
request.setType(SqlType.SQL_DELETE, word3, "DeleteSql");
|
||||
|
||||
} else if (word1.equalsIgnoreCase("INSERT")) {
|
||||
request.setType(SqlType.SQL_INSERT, word3, "InsertSql");
|
||||
|
||||
} else {
|
||||
request.setType(SqlType.SQL_UNKNOWN, null, "UnknownSql");
|
||||
|
||||
}
|
||||
if (updateSql.getTimeout() > 0) {
|
||||
pstmt.setQueryTimeout(updateSql.getTimeout());
|
||||
}
|
||||
|
||||
private void parseUpdate(String sql, PersistRequestUpdateSql request) {
|
||||
|
||||
int start = ltrim(sql);
|
||||
|
||||
int[] pos = new int[3];
|
||||
int spaceCount = 0;
|
||||
|
||||
int len = sql.length();
|
||||
for (int i = start; i < len; i++) {
|
||||
char c = sql.charAt(i);
|
||||
if (Character.isWhitespace(c)) {
|
||||
pos[spaceCount] = i;
|
||||
spaceCount++;
|
||||
if (spaceCount > 2){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String firstWord = sql.substring(0, pos[0]);
|
||||
String secWord = sql.substring(pos[0]+1, pos[1]);
|
||||
String thirdWord;
|
||||
if (pos[2] == 0){
|
||||
// there is nothing after the table name
|
||||
thirdWord = sql.substring(pos[1]+1);
|
||||
} else {
|
||||
thirdWord = sql.substring(pos[1]+1, pos[2]);
|
||||
}
|
||||
|
||||
determineType(firstWord, secWord, thirdWord, request);
|
||||
String bindLog = null;
|
||||
if (!bindParams.isEmpty()) {
|
||||
bindLog = binder.bind(bindParams, new DataBind(pstmt));
|
||||
}
|
||||
|
||||
private int ltrim(String s) {
|
||||
int len = s.length();
|
||||
int i = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!Character.isWhitespace(s.charAt(i))) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
request.setBindLog(bindLog);
|
||||
|
||||
// derive the statement type (for TransactionEvent)
|
||||
parseUpdate(sql, request);
|
||||
return pstmt;
|
||||
}
|
||||
|
||||
|
||||
private void determineType(String word1, String word2, String word3, PersistRequestUpdateSql request) {
|
||||
|
||||
if (word1.equalsIgnoreCase("UPDATE")) {
|
||||
request.setType(SqlType.SQL_UPDATE, word2, "UpdateSql");
|
||||
|
||||
} else if (word1.equalsIgnoreCase("DELETE")) {
|
||||
request.setType(SqlType.SQL_DELETE, word3, "DeleteSql");
|
||||
|
||||
} else if (word1.equalsIgnoreCase("INSERT")) {
|
||||
request.setType(SqlType.SQL_INSERT, word3, "InsertSql");
|
||||
|
||||
} else {
|
||||
request.setType(SqlType.SQL_UNKNOWN, null, "UnknownSql");
|
||||
}
|
||||
}
|
||||
|
||||
private void parseUpdate(String sql, PersistRequestUpdateSql request) {
|
||||
|
||||
int start = leadingTrim(sql);
|
||||
|
||||
int[] pos = new int[3];
|
||||
int spaceCount = 0;
|
||||
|
||||
int len = sql.length();
|
||||
for (int i = start; i < len; i++) {
|
||||
char c = sql.charAt(i);
|
||||
if (Character.isWhitespace(c)) {
|
||||
pos[spaceCount] = i;
|
||||
spaceCount++;
|
||||
if (spaceCount > 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String firstWord = sql.substring(0, pos[0]);
|
||||
String secWord = sql.substring(pos[0] + 1, pos[1]);
|
||||
String thirdWord;
|
||||
if (pos[2] == 0) {
|
||||
// there is nothing after the table name
|
||||
thirdWord = sql.substring(pos[1] + 1);
|
||||
} else {
|
||||
thirdWord = sql.substring(pos[1] + 1, pos[2]);
|
||||
}
|
||||
|
||||
determineType(firstWord, secWord, thirdWord, request);
|
||||
}
|
||||
|
||||
private int leadingTrim(String s) {
|
||||
int len = s.length();
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!Character.isWhitespace(s.charAt(i))) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -28,20 +28,16 @@ public class DeleteHandler extends DmlHandler {
|
||||
public void bind() throws SQLException {
|
||||
|
||||
sql = meta.getSql(persistRequest);
|
||||
|
||||
SpiTransaction t = persistRequest.getTransaction();
|
||||
boolean isBatch = t.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (isBatch) {
|
||||
if (persistRequest.isBatched()) {
|
||||
pstmt = getPstmt(t, sql, persistRequest, false);
|
||||
} else {
|
||||
pstmt = getPstmt(t, sql, false);
|
||||
}
|
||||
dataBind = new DataBind(pstmt);
|
||||
|
||||
meta.bind(persistRequest, this);
|
||||
|
||||
logSql(sql);
|
||||
}
|
||||
|
||||
@@ -51,9 +47,6 @@ public class DeleteHandler extends DmlHandler {
|
||||
public void execute() throws SQLException, OptimisticLockException {
|
||||
int rowCount = dataBind.executeUpdate();
|
||||
checkRowCount(rowCount);
|
||||
|
||||
// Deletes the bean from the PersistenceContext
|
||||
persistRequest.postDelete();
|
||||
}
|
||||
|
||||
public void registerDerivedRelationship(DerivedRelationshipData assocBean) {
|
||||
|
||||
@@ -69,35 +69,28 @@ public final class DmlBeanPersister implements BeanPersister {
|
||||
/**
|
||||
* execute request taking batching into account.
|
||||
*/
|
||||
private void execute(PersistRequest request, PersistHandler handler) {
|
||||
|
||||
SpiTransaction trans = request.getTransaction();
|
||||
boolean batchThisRequest = trans.isBatchThisRequest();
|
||||
private void execute(PersistRequestBean<?> request, PersistHandler handler) {
|
||||
|
||||
boolean batched = request.isBatched();
|
||||
try {
|
||||
|
||||
handler.bind();
|
||||
|
||||
if (batchThisRequest) {
|
||||
if (batched) {
|
||||
handler.addBatch();
|
||||
|
||||
} else {
|
||||
// immediate insert
|
||||
handler.execute();
|
||||
}
|
||||
|
||||
} 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+"]";
|
||||
if (request.getTransaction().isLogSummary()) {
|
||||
request.getTransaction().logSummary(msg);
|
||||
}
|
||||
|
||||
// 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+"]";
|
||||
if (request.getTransaction().isLogSummary()) {
|
||||
request.getTransaction().logSummary(msg);
|
||||
}
|
||||
throw new PersistenceException(msg, e);
|
||||
|
||||
} finally {
|
||||
if (!batchThisRequest && handler != null) {
|
||||
if (!batched && handler != null) {
|
||||
try {
|
||||
handler.close();
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -88,13 +88,12 @@ public class InsertHandler extends DmlHandler {
|
||||
}
|
||||
|
||||
SpiTransaction t = persistRequest.getTransaction();
|
||||
boolean isBatch = t.isBatchThisRequest();
|
||||
|
||||
// get the appropriate sql
|
||||
sql = meta.getSql(withId);
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (isBatch) {
|
||||
if (persistRequest.isBatched()) {
|
||||
pstmt = getPstmt(t, sql, persistRequest, useGeneratedKeys);
|
||||
} else {
|
||||
pstmt = getPstmt(t, sql, useGeneratedKeys);
|
||||
@@ -137,9 +136,7 @@ public class InsertHandler extends DmlHandler {
|
||||
}
|
||||
|
||||
checkRowCount(rc);
|
||||
//setAdditionalProperties();
|
||||
executeDerivedRelationships();
|
||||
|
||||
persistRequest.postInsert();
|
||||
}
|
||||
|
||||
|
||||
@@ -40,10 +40,9 @@ public class UpdateHandler extends DmlHandler {
|
||||
sql = updatePlan.getSql();
|
||||
|
||||
SpiTransaction t = persistRequest.getTransaction();
|
||||
boolean isBatch = t.isBatchThisRequest();
|
||||
|
||||
PreparedStatement pstmt;
|
||||
if (isBatch) {
|
||||
if (persistRequest.isBatched()) {
|
||||
pstmt = getPstmt(t, sql, persistRequest, false);
|
||||
} else {
|
||||
pstmt = getPstmt(t, sql, false);
|
||||
|
||||
Reference in new issue
Block a user