use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.
the class TestEmbeddedServer method testRemoteJDBCTrasport.
@Test
public void testRemoteJDBCTrasport() throws Exception {
SocketConfiguration s = new SocketConfiguration();
InetSocketAddress addr = new InetSocketAddress(0);
s.setBindAddress(addr.getHostName());
s.setPortNumber(addr.getPort());
s.setProtocol(WireProtocol.teiid);
EmbeddedConfiguration config = new EmbeddedConfiguration();
config.addTransport(s);
es.start(config);
es.deployVDB(new ByteArrayInputStream("<vdb name=\"test\" version=\"1\"><model name=\"test\" type=\"VIRTUAL\"><metadata type=\"DDL\"><![CDATA[CREATE VIEW helloworld as SELECT 'HELLO WORLD';]]> </metadata></model></vdb>".getBytes()));
Connection conn = null;
try {
TeiidDriver driver = new TeiidDriver();
conn = driver.connect("jdbc:teiid:test@mm://" + addr.getHostName() + ":" + es.transports.get(0).getPort(), null);
// trigger alternative serialization for byte count
conn.createStatement().execute("set showplan on");
ResultSet rs = conn.createStatement().executeQuery("select * from helloworld");
rs.next();
assertEquals("HELLO WORLD", rs.getString(1));
} finally {
if (conn != null) {
conn.close();
}
}
}
use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.
the class TransportService method start.
@Override
public void start(StartContext context) throws StartException {
this.setVDBRepository(this.getVdbRepository());
SessionService ss = sessionServiceInjector.getValue();
this.setSecurityHelper(ss.getSecurityHelper());
// create the necessary services
// $NON-NLS-1$
this.logon = new LogonImpl(ss, "teiid-cluster");
DQP dqpProxy = proxyService(DQP.class, getDQP(), LogConstants.CTX_DQP);
this.registerClientService(ILogon.class, logon, LogConstants.CTX_SECURITY);
this.registerClientService(DQP.class, dqpProxy, LogConstants.CTX_DQP);
this.setAuthenticationType(ss.getDefaultAuthenticationType());
if (this.socketConfig != null) {
/*
try {
// this is to show the bound socket port in the JMX console
SocketBinding socketBinding = getSocketBindingInjector().getValue();
ManagedServerSocketBinding ss = (ManagedServerSocketBinding)socketBinding.getSocketBindings().getServerSocketFactory().createServerSocket(socketBinding.getName());
socketBinding.getSocketBindings().getNamedRegistry().registerBinding(ss);
} catch (IOException e) {
throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
}
*/
this.address = getSocketBindingInjector().getValue().getSocketAddress();
this.socketConfig.setBindAddress(this.address.getHostName());
this.socketConfig.setPortNumber(this.address.getPort());
boolean sslEnabled = false;
if (this.socketConfig.getSSLConfiguration() != null) {
sslEnabled = this.socketConfig.getSSLConfiguration().isSslEnabled();
}
if (socketConfig.getProtocol() == WireProtocol.teiid) {
this.socketListener = new SocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue());
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50012, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled ? "ON" : "OFF")));
} else if (socketConfig.getProtocol() == WireProtocol.pg) {
TeiidDriver driver = new TeiidDriver();
driver.setLocalProfile(new ConnectionProfile() {
@Override
public ConnectionImpl connect(String url, Properties info) throws TeiidSQLException {
try {
LocalServerConnection sc = new LocalServerConnection(info, true) {
@Override
protected ClientServiceRegistry getClientServiceRegistry(String name) {
return TransportService.this;
}
};
return new ConnectionImpl(sc, info, url);
} catch (CommunicationException e) {
throw TeiidSQLException.create(e);
} catch (ConnectionException e) {
throw TeiidSQLException.create(e);
}
}
});
ODBCSocketListener odbc = new ODBCSocketListener(address, this.socketConfig, this, getBufferManagerInjector().getValue(), getMaxODBCLobSizeAllowed(), this.logon, driver);
this.socketListener = odbc;
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50037, this.transportName, address.getHostName(), String.valueOf(address.getPort()), (sslEnabled ? "ON" : "OFF")));
} else {
throw new StartException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50013));
}
} else {
LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50038, LocalServerConnection.jndiNameForRuntime(transportName)));
}
}
use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.
the class TestEmbeddedServer method testExternalMaterializationManagement.
@Test
public void testExternalMaterializationManagement() throws Exception {
EmbeddedConfiguration ec = new EmbeddedConfiguration();
ec.setUseDisk(false);
ec.setTransactionManager(SimpleMock.createSimpleMock(TransactionManager.class));
es.transactionService.setXaTerminator(SimpleMock.createSimpleMock(XATerminator.class));
es.transactionService.setWorkManager(new FakeWorkManager());
es.start(ec);
es.transactionService.setDetectTransactions(false);
final AtomicBoolean loaded = new AtomicBoolean();
final AtomicBoolean valid = new AtomicBoolean();
final AtomicInteger matTableCount = new AtomicInteger();
final AtomicInteger tableCount = new AtomicInteger();
final AtomicBoolean hasStatus = new AtomicBoolean();
es.addTranslator("y", new ExecutionFactory<AtomicInteger, Object>() {
public boolean supportsCompareCriteriaEquals() {
return true;
}
@Override
public Object getConnection(AtomicInteger factory) throws TranslatorException {
return factory.incrementAndGet();
}
@Override
public void closeConnection(Object connection, AtomicInteger factory) {
}
@Override
public void getMetadata(MetadataFactory metadataFactory, Object conn) throws TranslatorException {
assertEquals(conn, Integer.valueOf(1));
Table t = metadataFactory.addTable("my_table");
t.setSupportsUpdate(true);
Column c = metadataFactory.addColumn("my_column", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
// mat table
t = metadataFactory.addTable("mat_table");
t.setSupportsUpdate(true);
c = metadataFactory.addColumn("my_column", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
// status table
t = metadataFactory.addTable("status");
t.setSupportsUpdate(true);
c = metadataFactory.addColumn("VDBName", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("VDBVersion", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("SchemaName", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("Name", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("TargetSchemaName", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("TargetName", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("Valid", TypeFacility.RUNTIME_NAMES.BOOLEAN, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("LoadState", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("Cardinality", TypeFacility.RUNTIME_NAMES.LONG, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("Updated", TypeFacility.RUNTIME_NAMES.TIMESTAMP, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("LoadNumber", TypeFacility.RUNTIME_NAMES.LONG, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("NodeName", TypeFacility.RUNTIME_NAMES.STRING, t);
c.setUpdatable(true);
c = metadataFactory.addColumn("StaleCount", TypeFacility.RUNTIME_NAMES.LONG, t);
c.setUpdatable(true);
metadataFactory.addPrimaryKey("PK", Arrays.asList("VDBName", "VDBVersion", "SchemaName", "Name"), t);
}
@Override
public ResultSetExecution createResultSetExecution(final QueryExpression command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Object connection) throws TranslatorException {
return new ResultSetExecution() {
Iterator<? extends List<? extends Object>> results;
@Override
public void execute() throws TranslatorException {
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
String status = "SELECT status.TargetSchemaName, status.TargetName, status.Valid, " + "status.LoadState, status.Updated, status.Cardinality, status.LoadNumber " + "FROM status WHERE status.VDBName = 'test' AND status.VDBVersion = '1.0.0' " + "AND status.SchemaName = 'virt' AND status.Name = 'my_view'";
if (results == null) {
String commandString = command.toString();
if (hasStatus.get() && commandString.equals(status)) {
results = Arrays.asList(Arrays.asList(null, "mat_table", valid.get(), loaded.get() ? "LOADED" : "LOADING", new Timestamp(System.currentTimeMillis()), -1, new Integer(1))).iterator();
} else if (hasStatus.get() && commandString.startsWith("SELECT status.Valid, status.LoadState FROM status")) {
results = Arrays.asList(Arrays.asList(valid.get(), loaded.get() ? "LOADED" : "LOADING")).iterator();
} else if (loaded.get() && commandString.equals("SELECT mat_table.my_column FROM mat_table")) {
matTableCount.getAndIncrement();
results = Arrays.asList(Arrays.asList("mat_column0"), Arrays.asList("mat_column1")).iterator();
} else if (commandString.equals("SELECT my_table.my_column FROM my_table")) {
tableCount.getAndIncrement();
results = Arrays.asList(Arrays.asList("regular_column")).iterator();
}
}
if (results != null && results.hasNext()) {
return results.next();
}
return null;
}
};
}
@Override
public UpdateExecution createUpdateExecution(final Command command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Object connection) throws TranslatorException {
UpdateExecution ue = new UpdateExecution() {
@Override
public void execute() throws TranslatorException {
String commandString = command.toString();
if (commandString.startsWith("INSERT INTO status")) {
hasStatus.set(true);
}
if (commandString.startsWith("INSERT INTO status") || commandString.startsWith("UPDATE status SET")) {
if (commandString.contains("LoadState")) {
synchronized (loaded) {
loaded.set(commandString.indexOf("LOADED") != -1);
loaded.notifyAll();
}
}
if (commandString.contains("Valid")) {
valid.set(commandString.indexOf("TRUE") != -1);
}
}
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public int[] getUpdateCounts() throws DataNotAvailableException, TranslatorException {
return new int[] { 1 };
}
};
return ue;
}
});
final AtomicInteger counter = new AtomicInteger();
ConnectionFactoryProvider<AtomicInteger> cfp = new EmbeddedServer.SimpleConnectionFactoryProvider<AtomicInteger>(counter);
es.addConnectionFactoryProvider("z", cfp);
ModelMetaData mmd = new ModelMetaData();
mmd.setName("my_schema");
mmd.addSourceMapping("x", "y", "z");
ModelMetaData mmd1 = new ModelMetaData();
mmd1.setName("virt");
mmd1.setModelType(Type.VIRTUAL);
mmd1.setSchemaSourceType("ddl");
mmd1.setSchemaText(" create view my_view OPTIONS (" + "UPDATABLE 'true',MATERIALIZED 'TRUE',\n" + "MATERIALIZED_TABLE 'my_schema.mat_table', \n" + "\"teiid_rel:MATERIALIZED_STAGE_TABLE\" 'my_schema.mat_table',\n" + "\"teiid_rel:ALLOW_MATVIEW_MANAGEMENT\" 'true', \n" + "\"teiid_rel:MATVIEW_STATUS_TABLE\" 'my_schema.status', \n" + "\"teiid_rel:MATVIEW_AFTER_LOAD_SCRIPT\" 'select 1; select 1, ''a''', \n" + "\"teiid_rel:MATVIEW_SHARE_SCOPE\" 'NONE',\n" + "\"teiid_rel:MATVIEW_ONERROR_ACTION\" 'THROW_EXCEPTION',\n" + "\"teiid_rel:MATVIEW_TTL\" 100000)" + "as select * from \"my_table\";" + " create view mat_table as select 'I conflict';");
es.deployVDB("test", mmd, mmd1);
synchronized (loaded) {
while (!loaded.get()) {
loaded.wait();
}
}
// need to ensure that the mat view is built
Thread.sleep(2000);
final TeiidDriver td = es.getDriver();
Connection c = td.connect("jdbc:teiid:test", null);
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select * from my_view");
assertTrue(rs.next());
assertEquals("mat_column0", rs.getString(1));
s.execute("update my_schema.status set valid=false");
try {
rs = s.executeQuery("select * from my_view");
fail("expected throw exception to work");
} catch (SQLException e) {
}
assertEquals(1, tableCount.get());
// make sure a similar name doesn't cause an issue
rs = s.executeQuery("select * from (call sysadmin.updateMatView('virt', 'my_view', 'true')) as x");
rs.next();
assertEquals(2, rs.getInt(1));
assertEquals(2, tableCount.get());
s.execute("call setProperty((SELECT UID FROM Sys.Tables WHERE SchemaName = 'virt' AND Name = 'my_view'), 'teiid_rel:MATVIEW_ONERROR_ACTION', 'WAIT')");
// this thread should hang, until the status changes
final AtomicBoolean success = new AtomicBoolean();
Thread t = new Thread() {
public void run() {
try {
Connection c1 = td.connect("jdbc:teiid:test", null);
Statement s1 = c1.createStatement();
s1.executeQuery("select * from my_view");
success.set(true);
} catch (SQLException e) {
}
}
};
t.start();
// wait to ensure that the thread is blocked
Thread.sleep(5000);
// update the status and make sure the thread finished
s.execute("update my_schema.status set valid=true");
t.join(10000);
assertTrue(success.get());
}
use of org.teiid.jdbc.TeiidDriver 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());
}
}
use of org.teiid.jdbc.TeiidDriver in project teiid by teiid.
the class TestJDBCSocketTransport method testSyncTimeout.
/**
* Tests to ensure that a SynchronousTtl/synchTimeout does
* not cause a cancel.
* TODO: had to increase the values since the test would fail on slow machine runs
* @throws Exception
*/
@Test
public void testSyncTimeout() throws Exception {
TeiidDriver td = new TeiidDriver();
td.setSocketProfile(new ConnectionProfile() {
@Override
public ConnectionImpl connect(String url, Properties info) throws TeiidSQLException {
SocketServerConnectionFactory sscf = new SocketServerConnectionFactory();
sscf.initialize(info);
try {
return new ConnectionImpl(sscf.getConnection(info), info, url);
} catch (CommunicationException e) {
throw TeiidSQLException.create(e);
} catch (ConnectionException e) {
throw TeiidSQLException.create(e);
}
}
});
Properties p = new Properties();
p.setProperty("user", "testuser");
p.setProperty("password", "testpassword");
ConnectorManagerRepository cmr = server.getConnectorManagerRepository();
AutoGenDataService agds = new AutoGenDataService() {
@Override
public Object getConnectionFactory() throws TranslatorException {
return null;
}
};
// wait longer than the synch ttl/soTimeout, we should still succeed
agds.setSleep(2000);
cmr.addConnectorManager("source", agds);
try {
conn = td.connect("jdbc:teiid:parts@mm://" + addr.getHostName() + ":" + jdbcTransport.getPort(), p);
Statement s = conn.createStatement();
assertTrue(s.execute("select * from parts"));
} finally {
server.setConnectorManagerRepository(cmr);
}
}
Aggregations