Search in sources :

Example 16 with DataNotAvailableException

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

the class TestResultsCache method testScope.

@Test
public void testScope() throws Exception {
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("x");
    mmd.addProperty("teiid_rel:determinism", "USER_DETERMINISTIC");
    mmd.addSourceMapping("x", "x", null);
    mmd.addSourceMetadata("ddl", "create foreign table t (c string); create foreign procedure p () returns table (c string);");
    final AtomicBoolean setScope = new AtomicBoolean();
    server.addTranslator("x", new ExecutionFactory() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, final ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return createProcedureExecution(null, executionContext, metadata, connection);
        }

        @Override
        public ProcedureExecution createProcedureExecution(Call command, final ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ProcedureExecution() {

                boolean returned = false;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (setScope.get()) {
                        // prevent caching altogether
                        executionContext.setScope(Scope.SESSION);
                    }
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    return Arrays.asList(executionContext.getSession().getSessionId());
                }

                @Override
                public List<?> getOutputParameterValues() throws TranslatorException {
                    return null;
                }
            };
        }
    });
    server.deployVDB("x", mmd);
    Connection c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    String sessionid = rs.getString(1);
    // should be the same with same user/session
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    assertEquals(sessionid, rs.getString(1));
    c.close();
    c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    assertEquals(sessionid, rs.getString(1));
    c.close();
    // for the final test
    setScope.set(true);
    // should be different with another user
    c = server.getDriver().connect("jdbc:teiid:x;user=bill", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    String sessionid1 = rs.getString(1);
    c.close();
    assertNotEquals(sessionid, sessionid1);
    c = server.getDriver().connect("jdbc:teiid:x;user=bill", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ select * from t");
    assertTrue(rs.next());
    // scope session should prevent reuse
    assertNotEquals(sessionid1, rs.getString(1));
    setScope.set(false);
    rs = s.executeQuery("/* cache */ call p()");
    assertTrue(rs.next());
    sessionid = rs.getString(1);
    c.close();
    c = server.getDriver().connect("jdbc:teiid:x;user=alice", null);
    s = c.createStatement();
    rs = s.executeQuery("/* cache */ call p()");
    assertTrue(rs.next());
    assertNotEquals(sessionid, rs.getString(1));
    c.close();
}
Also used : Call(org.teiid.language.Call) Statement(java.sql.Statement) Connection(java.sql.Connection) ExecutionFactory(org.teiid.translator.ExecutionFactory) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ProcedureExecution(org.teiid.translator.ProcedureExecution) ResultSet(java.sql.ResultSet) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Aggregations

DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10 ExecutionContext (org.teiid.translator.ExecutionContext)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Test (org.junit.Test)9 TranslatorException (org.teiid.translator.TranslatorException)9 ResultSetExecution (org.teiid.translator.ResultSetExecution)5 SalesForceExecutionFactory (org.teiid.translator.salesforce.SalesForceExecutionFactory)5 SalesforceConnection (org.teiid.translator.salesforce.SalesforceConnection)5 IOException (java.io.IOException)4 Table (org.teiid.metadata.Table)4 JobInfo (com.sforce.async.JobInfo)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 ColumnReference (org.teiid.language.ColumnReference)3 Command (org.teiid.language.Command)3 Expression (org.teiid.language.Expression)3 ExpressionValueSource (org.teiid.language.ExpressionValueSource)3 Insert (org.teiid.language.Insert)3