Search in sources :

Example 6 with TranslatorBatchException

use of org.teiid.translator.TranslatorBatchException in project teiid by teiid.

the class TestEmbeddedServer method testBatchedUpdateErrors.

@Test
public void testBatchedUpdateErrors() throws Exception {
    EmbeddedConfiguration ec = new EmbeddedConfiguration();
    ec.setUseDisk(false);
    es.start(ec);
    MockTransactionManager tm = new MockTransactionManager();
    ec.setTransactionManager(tm);
    HardCodedExecutionFactory hcef = new HardCodedExecutionFactory() {

        @Override
        public boolean supportsCompareCriteriaEquals() {
            return true;
        }
    };
    hcef.addUpdate("UPDATE pm1.g1 SET e1 = 'a' WHERE pm1.g1.e2 = 1", new int[] { 1 });
    hcef.addUpdate("UPDATE pm1.g1 SET e1 = 'b' WHERE pm1.g1.e2 = 2", new TranslatorException("i've failed"));
    es.addTranslator("y", hcef);
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("my-schema");
    mmd.addSourceMapping("x", "y", null);
    mmd.addSourceMetadata("ddl", "create foreign table \"pm1.g1\" (e1 string, e2 integer) options (updatable true)");
    es.deployVDB("test", mmd);
    TeiidDriver td = es.getDriver();
    Connection c = td.connect("jdbc:teiid:test", null);
    Statement s = c.createStatement();
    // $NON-NLS-1$
    s.addBatch("update pm1.g1 set e1 = 'a' where e2 = 1");
    // $NON-NLS-1$
    s.addBatch("update pm1.g1 set e1 = 'b' where e2 = 2");
    try {
        s.executeBatch();
        fail();
    } catch (BatchUpdateException e) {
        int[] updateCounts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1 }, updateCounts);
        assertEquals(-1, s.getUpdateCount());
    }
    // redeploy with batch support
    hcef = new HardCodedExecutionFactory() {

        @Override
        public boolean supportsCompareCriteriaEquals() {
            return true;
        }

        @Override
        public boolean supportsBatchedUpdates() {
            return true;
        }
    };
    es.addTranslator("z", hcef);
    es.undeployVDB("test");
    mmd = new ModelMetaData();
    mmd.setName("my-schema");
    mmd.addSourceMetadata("ddl", "create foreign table \"pm1.g1\" (e1 string, e2 integer) options (updatable true)");
    mmd.addSourceMapping("y", "z", null);
    es.deployVDB("test", mmd);
    c = td.connect("jdbc:teiid:test", null);
    s = c.createStatement();
    // $NON-NLS-1$
    s.addBatch("update pm1.g1 set e1 = 'a' where e2 = 1");
    // $NON-NLS-1$
    s.addBatch("update pm1.g1 set e1 = 'b' where e2 = 2");
    hcef.updateMap.clear();
    hcef.addUpdate("UPDATE pm1.g1 SET e1 = 'a' WHERE pm1.g1.e2 = 1;\nUPDATE pm1.g1 SET e1 = 'b' WHERE pm1.g1.e2 = 2;", new TranslatorBatchException(new SQLException(), new int[] { 1, -3 }));
    try {
        s.executeBatch();
        fail();
    } catch (BatchUpdateException e) {
        int[] updateCounts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1, -3 }, updateCounts);
        assertEquals(-1, s.getUpdateCount());
    }
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) TranslatorException(org.teiid.translator.TranslatorException) TeiidDriver(org.teiid.jdbc.TeiidDriver) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Aggregations

TranslatorBatchException (org.teiid.translator.TranslatorBatchException)6 BatchUpdateException (java.sql.BatchUpdateException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Insert (org.teiid.language.Insert)3 Connection (java.sql.Connection)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)2 BulkCommand (org.teiid.language.BulkCommand)2 TranslatorException (org.teiid.translator.TranslatorException)2 Statement (java.sql.Statement)1 GeneratedKeys (org.teiid.GeneratedKeys)1 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)1 BlockedException (org.teiid.common.buffer.BlockedException)1 TupleBatch (org.teiid.common.buffer.TupleBatch)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1