Search in sources :

Example 6 with TransactionService

use of org.teiid.dqp.service.TransactionService in project teiid by teiid.

the class BatchedUpdatePlan method close.

/**
 * @see org.teiid.query.processor.ProcessorPlan#close()
 * @since 4.2
 */
public void close() throws TeiidComponentException {
    // if the plan opened but the atomic request got cancelled then close the last plan node.
    TransactionService ts = this.getContext().getTransactionServer();
    if (planIndex < updatePlans.length && planOpened[planIndex]) {
        try {
            updatePlans[planIndex].close();
        } catch (TeiidComponentException e) {
            LogManager.logWarning(LogConstants.CTX_DQP, e, e.getMessage());
        }
        if (this.planContexts[planIndex] != null) {
            try {
                ts.resume(this.planContexts[planIndex]);
                ts.rollback(this.planContexts[planIndex]);
            } catch (XATransactionException e) {
                LogManager.logWarning(LogConstants.CTX_DQP, e, e.getMessage());
            }
            this.planContexts[planIndex] = null;
        }
    }
}
Also used : TransactionService(org.teiid.dqp.service.TransactionService) TeiidComponentException(org.teiid.core.TeiidComponentException) XATransactionException(org.teiid.client.xa.XATransactionException)

Example 7 with TransactionService

use of org.teiid.dqp.service.TransactionService 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);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) TransactionService(org.teiid.dqp.service.TransactionService) TransactionContext(org.teiid.dqp.service.TransactionContext) TupleBatch(org.teiid.common.buffer.TupleBatch) Test(org.junit.Test)

Example 8 with TransactionService

use of org.teiid.dqp.service.TransactionService 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);
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) TransactionService(org.teiid.dqp.service.TransactionService) FakeDataManager(org.teiid.query.processor.FakeDataManager) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TransactionContext(org.teiid.dqp.service.TransactionContext) List(java.util.List) ArrayList(java.util.ArrayList) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Test(org.junit.Test)

Example 9 with TransactionService

use of org.teiid.dqp.service.TransactionService 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));
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) TransactionService(org.teiid.dqp.service.TransactionService) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) TransactionContext(org.teiid.dqp.service.TransactionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) Test(org.junit.Test)

Aggregations

TransactionService (org.teiid.dqp.service.TransactionService)9 TransactionContext (org.teiid.dqp.service.TransactionContext)6 CommandContext (org.teiid.query.util.CommandContext)5 List (java.util.List)4 Test (org.junit.Test)4 TeiidComponentException (org.teiid.core.TeiidComponentException)4 XATransactionException (org.teiid.client.xa.XATransactionException)3 TupleBatch (org.teiid.common.buffer.TupleBatch)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 ArrayList (java.util.ArrayList)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)2 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)2 VariableContext (org.teiid.query.sql.util.VariableContext)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Transaction (javax.transaction.Transaction)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1