use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.
the class TestBatchedUpdatePlan method testCommandLevelTransaction.
@Test
public void testCommandLevelTransaction() throws Exception {
// $NON-NLS-1$
CommandContext context = new CommandContext("pID", null, null, null, 1);
context.setMetadata(RealMetadataFactory.example1Cached());
TransactionContext tc = new TransactionContext();
TransactionService ts = Mockito.mock(TransactionService.class);
context.setTransactionService(ts);
context.setTransactionContext(tc);
ProcessorPlan[] plans = new ProcessorPlan[4];
for (int i = 0; i < plans.length - 2; i++) {
plans[i] = new FakeProcessorPlan(1);
}
for (int i = 2; i < plans.length; i++) {
plans[i] = new FakeProcessorPlan(1) {
public Boolean requiresTransaction(boolean transactionalReads) {
return true;
}
};
}
BatchedUpdatePlan plan = new BatchedUpdatePlan(Arrays.asList(plans), plans.length, null, false);
plan.requiresTransaction(false);
plan.initialize(context, null, null);
plan.open();
TupleBatch batch = plan.nextBatch();
assertEquals(4, batch.getRowCount());
Mockito.verify(ts, Mockito.times(2)).begin(tc);
Mockito.verify(ts, Mockito.times(2)).commit(tc);
}
use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.
the class TestProcedureProcessor method testBeginAtomic.
@Test
public void testBeginAtomic() throws Exception {
String proc = // $NON-NLS-1$
"CREATE VIRTUAL PROCEDURE " + // $NON-NLS-1$
"BEGIN ATOMIC" + // $NON-NLS-1$
" select e1, e2, e3, e4 into #t1 from pm1.g1;\n" + // $NON-NLS-1$
" update #t1 set e1 = 1 where e4 < 2;\n" + // $NON-NLS-1$
" delete from #t1 where e4 > 2;\n" + // $NON-NLS-1$
" select e2/\"in\" from #t1;\n" + // $NON-NLS-1$
"END";
TransformationMetadata tm = RealMetadataFactory.example1();
addProc(tm, "sq1", proc, new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER }, new String[] { "in" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER });
FakeDataManager dataMgr = exampleDataManager(tm);
// $NON-NLS-1$
CommandContext context = new CommandContext("pID", null, null, null, 1);
QueryMetadataInterface metadata = new TempMetadataAdapter(tm, new TempMetadataStore());
context.setMetadata(metadata);
TransactionContext tc = new TransactionContext();
TransactionService ts = Mockito.mock(TransactionService.class);
context.setTransactionService(ts);
context.setTransactionContext(tc);
// $NON-NLS-1$
String userQuery = "EXEC pm1.sq1(1)";
ProcessorPlan plan = getProcedurePlan(userQuery, tm, TestOptimizer.getGenericFinder());
List[] expected = new List[] { Arrays.asList(5) };
TestProcessor.helpProcess(plan, context, dataMgr, expected);
Mockito.verify(ts, Mockito.times(3)).begin(tc);
Mockito.verify(ts, Mockito.times(3)).commit(tc);
tc = new TransactionContext();
ts = Mockito.mock(TransactionService.class);
context.setTransactionService(ts);
context.setTransactionContext(tc);
// $NON-NLS-1$
userQuery = "EXEC pm1.sq1(0)";
plan = getProcedurePlan(userQuery, tm, TestOptimizer.getGenericFinder());
expected = null;
try {
TestProcessor.helpProcess(plan, context, dataMgr, expected);
fail();
} catch (TeiidProcessingException e) {
}
Mockito.verify(ts).begin(tc);
Mockito.verify(ts, Mockito.times(2)).resume(tc);
Mockito.verify(ts, Mockito.times(0)).commit(tc);
Mockito.verify(ts).rollback(tc);
}
use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.
the class TestTempTables method setupTransaction.
private void setupTransaction(int isolation) throws RollbackException, SystemException {
txn = Mockito.mock(Transaction.class);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
synch = (Synchronization) invocation.getArguments()[0];
return null;
}
}).when(txn).registerSynchronization((Synchronization) Mockito.anyObject());
Mockito.stub(txn.toString()).toReturn("txn");
tc = new TransactionContext();
tc.setTransaction(txn);
tc.setIsolationLevel(isolation);
tc.setTransactionType(Scope.REQUEST);
}
use of org.teiid.dqp.service.TransactionContext in project teiid by teiid.
the class TestTriggerActions method testTransactions.
@Test
public void testTransactions() throws Exception {
TransformationMetadata metadata = RealMetadataFactory.fromDDL("x", new RealMetadataFactory.DDLHolder("virt", "CREATE VIEW users options (updatable true) as select id, dummy_data, updated_at from db1.user1 union all select id, dummy_data, updated_at from db2.user2; " + "CREATE TRIGGER ON users INSTEAD OF DELETE AS FOR EACH ROW IF (OLD.id < (SELECT MAX(id) FROM db1.user1)) BEGIN ATOMIC DELETE FROM db1.user1 WHERE id = OLD.id; END ELSE IF (OLD.id = (SELECT MAX(id) FROM db1.user1)) BEGIN ATOMIC UPDATE db1.user1 SET dummy_data = '', updated_at = NOW() WHERE id = OLD.id; END ELSE IF (OLD.id > (SELECT MAX(id) FROM db1.user1)) BEGIN ATOMIC DELETE FROM db2.user2 WHERE id = OLD.id; END; " + "CREATE TRIGGER ON users INSTEAD OF INSERT AS FOR EACH ROW begin atomic insert into db1.user1 (id) values (new.id); insert into db2.user2 (id) values (new.id); end; " + "CREATE TRIGGER ON users INSTEAD OF update AS FOR EACH ROW begin atomic end; "), new RealMetadataFactory.DDLHolder("db1", "create foreign table user1 (id integer, dummy_data string, updated_at timestamp) options (updatable true);"), new RealMetadataFactory.DDLHolder("db2", "create foreign table user2 (id integer, dummy_data string, updated_at timestamp) options (updatable true);"));
String sql = "delete from users where id = 203";
HardcodedDataManager dm = new HardcodedDataManager();
dm.addData("SELECT g_0.id, g_0.dummy_data, g_0.updated_at FROM db1.user1 AS g_0 WHERE g_0.id = 203", Arrays.asList(203, "", null));
dm.addData("SELECT g_0.id, g_0.dummy_data, g_0.updated_at FROM db2.user2 AS g_0 WHERE g_0.id = 203");
dm.addData("SELECT g_0.id FROM db1.user1 AS g_0", Arrays.asList(203), Arrays.asList(204));
dm.addData("DELETE FROM db1.user1 WHERE id = 203", Arrays.asList(1));
dm.addData("INSERT INTO db1.user1 (id) VALUES (205)", Arrays.asList(1));
dm.addData("INSERT INTO db2.user2 (id) VALUES (205)", Arrays.asList(1));
CommandContext context = createCommandContext();
BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
caps.setSourceProperty(Capability.TRANSACTION_SUPPORT, TransactionSupport.XA);
ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
// assumed required, but won't start a txn
assertTrue(plan.requiresTransaction(false));
TransactionContext tc = new TransactionContext();
TransactionService ts = Mockito.mock(TransactionService.class);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((TransactionContext) invocation.getArguments()[0]).setTransactionType(Scope.REQUEST);
return null;
}
}).when(ts).begin(tc);
context.setTransactionService(ts);
context.setTransactionContext(tc);
List<?>[] expected = new List[] { Arrays.asList(1) };
helpProcess(plan, context, dm, expected);
Mockito.verify(ts, Mockito.never()).begin(tc);
// required, and will start a txn
sql = "insert into users (id) values (205)";
plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
assertTrue(plan.requiresTransaction(false));
helpProcess(plan, context, dm, expected);
Mockito.verify(ts, Mockito.times(1)).begin(tc);
// does nothing
sql = "update users set id = 205";
plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
assertFalse(plan.requiresTransaction(false));
}
Aggregations