Friday, August 17, 2012

Embedded Teiid

When developing a new Teiid translators, I found very convenient to use a new 8.1 feature: Embedded Teiid. Basically this is a working Teiid server, which does not require JBoss AS as a host platform. Of course some functionality provided by Application Server has to be added manually (like transactions, clustering or data connection pooling), but in many cases it is unnecessary, especially when you need just a simple federated database or have to quickly test your new translator.

Basic functionality can be unit-tested with frameworks like Mockito. But only integration test can give you a full picture. For integration tests I have to run a JBoss and Teiid instance as Cargo container with translator jars deployed - basically a mini-scaled copy of our application. This is a fragile and slow way - you have to emulate the environment on which your application is running, provide a proper configs and data sources, make sure the integration tests executed as part of your local build. Better if you have a flexibility of unit tests and completeness of integration tests combined together.

Below is a sample code how to instantiate an Embedded server:
 EmbeddedConfiguration ec = new EmbeddedConfiguration();  
 server = new EmbeddedServer();  
 server.start(ec);  
 final JdbcDataSource h2ds = new JdbcDataSource();  
 h2ds.setURL("jdbc:h2:file:target/accounts");  
 EmbeddedServer.ConnectionFactoryProvider<DataSource> jdbcProvider =   
 new EmbeddedServer.ConnectionFactoryProvider<DataSource>()   
 {  
      public DataSource getConnectionFactory() throws TranslatorException   
      {  
           return h2ds;  
      }  
 };  
 server.addConnectionFactoryProvider("source-jdbc", jdbcProvider);  
 server.addTranslator(new H2ExecutionFactory());  
 ModelMetaData jdbcModel = new ModelMetaData();  
 jdbcModel.setName("Accounts");  
 jdbcModel.setSchemaSourceType("native");  
 jdbcModel.addSourceMapping("h2-connector", "h2", "source-jdbc");       
 teiidServer.deployVDB("example", jdbcModel);  

Create server, define model and data sources, deploy VDB - in a less then twenty lines of code. Now you can connect and run your queries:
 TeiidDriver td = server.getDriver();  
 Connection c = td.connect("jdbc:teiid:example", null);  
 final String sql = "select * from Product";            
 List<String> results = execute(c, sql, true);  

Really, cannot be simpler. The complete listing of unit-test class attached for reference. Also take a look on "embedded-portfolio" example from Teiid 8.1 sources, my post was greatly inspired by it.

4 comments:

  1. This is great news! Makes developing translators much easier

    ReplyDelete
  2. Hello Andriy,

    I am having great difficulty with integrating TEIID into Eclipse Juno. Version 7.8.0 loads, from a local repository into Eclipse, via "Install new Software", by just pointing to zip file. This does not work for 8.0, it complains about not being able to find the jar file.

    I am interested in the embedded mode, I have also been struggling myself silly with starting a TEIID instance, when running JBoss. It just refuses, with a connection refusal to localhost:31443.

    What approach would you take, as I find myself meandering through endless configurations and compatibility issues.

    Thanks.

    ReplyDelete
  3. Hi Drikus,

    I think your first question is about Teiid Designer. This is a separate (non-mandatory) product from Teiid. I do not use it, but you can get a better support if you post your question with all necessary details on Teiid Designer community forum: https://community.jboss.org/en/teiiddesigner?view=discussions

    About your second question: you do not need JBoss to run Teiid in Embedded mode. Please take a proper look on my unit test case attached to this blog post. All what you need is pom.xml with proper dependencies (if you use Maven), then "mvn test" should do the rest.

    ReplyDelete
  4. Thanks Andriy.

    I'll post on the relevant forum for JBoss. I do realise that embedded, is embedded, i.e. not using an application server, and I am using Maven, so I'll jump on pom.xml like a flea on a camel. I appreciate your answer.

    ReplyDelete

Note: Only a member of this blog may post a comment.