use of herddb.model.TransactionContext in project herddb by diennea.
the class SimpleTransactionTest method testInsertInsert.
@Test
public void testInsertInsert() throws Exception {
Bytes key = Bytes.from_string("key1");
{
Record record = new Record(key, Bytes.from_int(0));
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
long tx = beginTransaction();
{
Record record = new Record(key, Bytes.from_int(1));
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
try {
manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount();
fail();
} catch (DuplicatePrimaryKeyException expected) {
}
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class SimpleTransactionTest method testRollbackUpdate1.
@Test
public void testRollbackUpdate1() throws Exception {
Bytes key = Bytes.from_string("key1");
Record record = new Record(key, Bytes.from_int(0));
InsertStatement st_insert = new InsertStatement(tableSpace, tableName, record);
assertEquals(1, manager.executeUpdate(st_insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
long tx = beginTransaction();
Record record2 = new Record(key, Bytes.from_int(1));
UpdateStatement st_update = new UpdateStatement(tableSpace, tableName, record2, null);
assertEquals(1, manager.executeUpdate(st_update, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
rollbackTransaction(tx);
GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(get.found());
assertEquals(get.getRecord().value, record.value);
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DropTableSQLTest method dropTableWithTransaction.
@Test
public void dropTableWithTransaction() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList(), new TransactionContext(tx));
execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), new TransactionContext(tx));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
long tx2 = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "DROP TABLE tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx2));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx2, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
assertTrue(manager.getPlanner() instanceof SQLPlanner);
} catch (StatementExecutionException ok) {
assertEquals("From line 1, column 15 to line 1, column 28: Object 'TSQL' not found within 'tblspace1'", ok.getMessage());
assertTrue(manager.getPlanner() instanceof CalcitePlanner);
}
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DBManager method executePlan.
public StatementExecutionResult executePlan(ExecutionPlan plan, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
context.setManager(this);
plan.validateContext(context);
if (plan.mainStatement instanceof ScanStatement) {
DataScanner result = scan((ScanStatement) plan.mainStatement, context, transactionContext);
// transction can be auto generated during the scan
transactionContext = new TransactionContext(result.transactionId);
return executeDataScannerPlan(plan, result, context, transactionContext);
} else if (plan.dataSource != null) {
// INSERT from SELECT
try {
ScanResult data = (ScanResult) executePlan(plan.dataSource, context, transactionContext);
int insertCount = 0;
try {
// transction can be auto generated during the scan
transactionContext = new TransactionContext(data.transactionId);
while (data.dataScanner.hasNext()) {
DataAccessor tuple = data.dataScanner.next();
SQLStatementEvaluationContext tmp_context = new SQLStatementEvaluationContext("--", Arrays.asList(tuple.getValues()));
DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(plan.mainStatement, tmp_context, transactionContext);
insertCount += res.getUpdateCount();
}
} finally {
data.dataScanner.close();
}
return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
} else if (plan.joinStatements != null) {
List<DataScanner> scanResults = new ArrayList<>();
for (ScanStatement statement : plan.joinStatements) {
DataScanner result = scan(statement, context, transactionContext);
// transction can be auto generated during the scan
transactionContext = new TransactionContext(result.transactionId);
scanResults.add(result);
}
return executeJoinedScansPlan(scanResults, context, transactionContext, plan);
} else if (plan.insertStatements != null) {
int insertCount = 0;
for (InsertStatement insert : plan.insertStatements) {
DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(insert, context, transactionContext);
// transction can be auto generated during the loop
transactionContext = new TransactionContext(res.transactionId);
insertCount += res.getUpdateCount();
}
return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
} else {
return executeStatement(plan.mainStatement, context, transactionContext);
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class TableSpaceManager method scan.
public DataScanner scan(ScanStatement statement, StatementEvaluationContext context, TransactionContext transactionContext, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
boolean rollbackOnError = false;
if (transactionContext.transactionId == TransactionContext.AUTOTRANSACTION_ID) {
StatementExecutionResult newTransaction = beginTransaction();
transactionContext = new TransactionContext(newTransaction.transactionId);
rollbackOnError = true;
}
Transaction transaction = transactions.get(transactionContext.transactionId);
if (transaction != null && !transaction.tableSpace.equals(tableSpaceName)) {
throw new StatementExecutionException("transaction " + transaction.transactionId + " is for tablespace " + transaction.tableSpace + ", not for " + tableSpaceName);
}
try {
String table = statement.getTable();
AbstractTableManager tableManager = tables.get(table);
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName);
}
if (tableManager.getCreatedInTransaction() > 0) {
if (transaction == null || transaction.transactionId != tableManager.getCreatedInTransaction()) {
throw new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + tableManager.getCreatedInTransaction());
}
}
return tableManager.scan(statement, context, transaction, lockRequired, forWrite);
} catch (StatementExecutionException error) {
if (rollbackOnError) {
rollbackTransaction(new RollbackTransactionStatement(tableSpaceName, transactionContext.transactionId));
}
throw error;
}
}
Aggregations