use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class Session method setVariable.
/**
* Set the value of the given variable for this session.
*
* @param name the name of the variable (may not be null)
* @param value the new value (may not be null)
*/
public void setVariable(String name, Value value) {
initVariables();
modificationId++;
Value old;
if (value == ValueNull.INSTANCE) {
old = variables.remove(name);
} else {
old = variables.put(name, value);
}
if (old != null) {
// close the old value (in case it is a lob)
old.close();
}
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class Session method commit.
/**
* Commit the current transaction. If the statement was not a data
* definition statement, and if there are temporary tables that should be
* dropped or truncated at commit, this is done as well.
*
* @param ddl if the statement was a data definition statement
*/
public void commit(boolean ddl) {
currentTransactionName = null;
transactionStart = 0;
if (containsUncommitted()) {
// need to commit even if rollback is not possible
// (create/drop table and so on)
database.commit(this);
}
if (temporaryLobs != null) {
for (Value v : temporaryLobs) {
v.close();
}
temporaryLobs.clear();
}
if (!ddl) {
// do not clean the temp tables if the last command was a
// create/drop
cleanTempTables(false);
if (autoCommitAtTransactionEnd) {
autoCommit = true;
autoCommitAtTransactionEnd = false;
}
}
endTransaction();
boolean commit = true;
List<SQLException> commitExceptions = New.arrayList();
StringBuilder buf = new StringBuilder();
for (Map.Entry<String, Connection> entry : connectionHolder.entrySet()) {
if (commit) {
try {
entry.getValue().commit();
buf.append("\ncommit shard " + entry.getKey() + " transaction succeed.");
} catch (SQLException ex) {
commit = false;
commitExceptions.add(ex);
buf.append("\ncommit shard " + entry.getKey() + " transaction failure.");
}
} else {
// remaining connections
try {
entry.getValue().rollback();
buf.append("\nrollback shard " + entry.getKey() + " transaction succeed.");
} catch (SQLException ex) {
buf.append("\nrollback shard " + entry.getKey() + " transaction failure.");
}
}
}
if (commitExceptions.isEmpty()) {
trace.debug("commit multiple group transaction succeed. commit track list:{0}", buf);
} else {
trace.error(commitExceptions.get(0), "fail to commit multiple group transaction. commit track list:{0}", buf);
DbException.convert(commitExceptions.get(0));
}
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class BatchUpdateWorker method error.
/**
* @param e
*/
protected void error(Throwable e) {
StatementBuilder buff = new StatementBuilder();
buff.append(shardName).append(" executing batchUpdate error:").append(sql);
for (List<Value> params : array) {
if (params != null) {
if (params != null && params.size() > 0) {
buff.appendExceptFirst(", ");
buff.append("\n{");
int i = 1;
for (Value v : params) {
buff.appendExceptFirst(", ");
buff.append(i++).append(": ").append(v.getSQL());
}
buff.append('}');
}
}
}
buff.append(';');
trace.error(e, buff.toString());
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class PreparedRoutingExecutor method buildInsert.
/**
* build insert statement
* @param forTable
* @param columns
* @param row
* @param buff
* @return
*/
protected List<Value> buildInsert(String forTable, Column[] columns, SearchRow row, StatementBuilder buff) {
ArrayList<Value> params = New.arrayList();
buff.append("INSERT INTO ");
buff.append(identifier(forTable)).append('(');
for (Column c : columns) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL());
}
buff.append(") ");
buff.resetCount();
buff.append("VALUES( ");
for (int i = 0; i < columns.length; i++) {
Value v = row.getValue(i);
buff.appendExceptFirst(", ");
if (v == null) {
buff.append("DEFAULT");
} else if (isNull(v)) {
buff.append("NULL");
} else {
buff.append('?');
params.add(v);
}
}
buff.append(")");
return params;
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class PreparedRoutingExecutor method invokeUpdateRow.
/**
* @param result
* @param row
* @return
*/
private int invokeUpdateRow(RoutingResult result, Row row) {
List<JdbcWorker<Integer>> workers = New.arrayList(result.tableNodeCount());
TableNode[] selectNodes = result.getSelectNodes();
for (TableNode node : selectNodes) {
StatementBuilder sqlBuff = new StatementBuilder();
List<Value> params = doTranslate(node, row, sqlBuff);
workers.add(createUpdateWorker(node.getShardName(), sqlBuff.toString(), params));
}
try {
addRuningJdbcWorkers(workers);
int affectRows = 0;
if (workers.size() > 1) {
// MILLISECONDS
int queryTimeout = getQueryTimeout();
List<Future<Integer>> invokeAll;
if (queryTimeout > 0) {
invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
} else {
invokeAll = jdbcExecutor.invokeAll(workers);
}
for (Future<Integer> future : invokeAll) {
affectRows += future.get();
}
} else if (workers.size() == 1) {
affectRows = workers.get(0).doWork();
}
return affectRows;
} catch (InterruptedException e) {
throw DbException.convert(e);
} catch (ExecutionException e) {
throw DbException.convert(e.getCause());
} finally {
removeRuningJdbcWorkers(workers);
for (JdbcWorker<Integer> jdbcWorker : workers) {
jdbcWorker.closeResource();
}
}
}
Aggregations