SlideShare a Scribd company logo
1 of 20
SQLJ for Developers Andrea Parrilli Oracle SQL Expert, Developer Mvn: thanks to Damjan Kumar
Topics ,[object Object]
Short SQLJ syntax introduction;
Equivalence between SQLJ and JDBC;
SQLJ on DB2 using Data Studio Developer, setup, binding, automatic building with maven;
Performance comparison between SQLJ, unoptimized JDBC, JDBC with PreparedStatements;
Performance comparison: SQL PL vs. Java,[object Object]
It is specified by ISO/IEC in module SQL/OLB to support portability.
The SQLJ preprocessor produces JDBC code with precompiled prepared statements.,[object Object]
The Generated Code sqlj.runtime.ConnectionContext __sJT_connCtx = ctx;   if (__sJT_connCtx == null) 	sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX();   sqlj.runtime.ExecutionContext __sJT_execCtx = 	__sJT_connCtx.getExecutionContext();   if (__sJT_execCtx == null) 	sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX();   synchronized (__sJT_execCtx) {     sqlj.runtime.profile.RTStatement __sJT_stmt = 	__sJT_execCtx.registerStatement(__sJT_connCtx, 				MAPLOG_SJProfileKeys.getKey(0), 2);     try      {       __sJT_stmt.setString(1, msg);       __sJT_execCtx.executeUpdate();     }     finally      {       __sJT_execCtx.releaseStatement();     }   }
SQLJ Syntax ,[object Object]
DQL, DML & DDL: supports the full syntax of the underlying database manager.
In the IBM implementation of SQLJ standard the statements are exclusively processed at compile time (the bind process), this means that the host language variables can appear only where a bind parameter in a prepared statement (JDBC or standard SQL’s procedural language extensions EXECUTE … USING) can, thus allowing only static SQL in SQLJ statements. On the contrary Oracle’s implementation allows for dynamic SQL in a very intuitive way but at the same time offers less features for what concerns iterators (this is mainly due to the differences between the two transaction models and the cursors implentations). ,[object Object]
SQLJ Syntax Iterators #sql [context] iterator <it_name>  	implements sqlj.runtime.ForUpdate, 		sqlj.runtime.Scrollable 	with(holdability=… 		  sensitivity=… 		  updateColumns=“…”) { 	<JavaDataType> <field_name>, 	… } The standard defines datatypes mappings between Java and SQL: ex. INTEGER is mapped to BigDecimal, not to BigInteger!
Why Maven? Manages library dependencies; Manages library download and installation; Manages the build process: SQLJ translation of .sqlj files into .java; Binding .ser files to the database; Generation of artifacts (.jar, .war, …); Execution from command line (calling the main method with the right classpath); Maven is Open Source! Once configured allows for a greater productivity shortening deploy and setup times and it fully integrates with the versioning system in use.
How do we manage library dependencies Pom.xml Manual install <dependency> 	<groupId>com.ibm.db2</groupId> 	<artifactId>db2jcc</artifactId> 	<version>1.0</version> </dependency> <dependency> 	<groupId>jdom</groupId> 	<artifactId>jdom</artifactId> 	<version>1.1</version> </dependency> <dependency> 	<groupId>org.springframework</groupId> 	<artifactId>spring-core</artifactId> 	<version>2.5.6</version> </dependency> mvn install:install-file -Dfile=lib/db2jcc.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc -Dversion=1.0 -Dpackaging=jar -Dgeneratepom=true Not everything is in the standard repositories!
Maven: SQLJ translation of .sqlj into .java We use a custom maven plugin (sqlj-maven-plugin) for translation and binding of SQLJ files. <build>    <plugins>       <plugin>          <groupId>si.srcsi</groupId>          <artifactId>sqlj-maven-plugin</artifactId>          <version>1.2</version>          <executions>             <execution>                <goals>                   <goal>sqljex</goal>                   <goal>sqljcustomize</goal>                </goals>                <configuration>                   <sqljDirs>                      <sqljDir>src/main/java</sqljDir>                   </sqljDirs>                   <generatedSourcesDirectory>src/main/java</generatedSourcesDirectory>                   <generatedResourcesDirectory>src/main/java</generatedResourcesDirectory>                   <classpath>lib/framework.jar</classpath>                   <dbUrl>${dbUrl}</dbUrl>                   <dbUser>${dbUser}</dbUser>                   <dbPassword>${dbPassword}</dbPassword>                   <dbCollection>${dbCollection}</dbCollection>                </configuration>             </execution>          </executions>       </plugin>    </plugins> </build>
Maven: SQLJ translation the implementation public class SqljExMojo extends AbstractSqljMojo{     private File[] sqljFiles;     private File[] sqljDirs;     public void execute() {      ...      Set sqljFiles = getSqljFiles();      for ( Iterator i=sqljFiles.iterator(); i.hasNext(); ){             File file = (File) i.next();             generate( file );         }         Resource resource = new Resource();         resource.setDirectory( getGeneratedResourcesDirectory().getAbsolutePath() );         mavenProject.addResource( resource );         mavenProject.addCompileSourceRoot( getGeneratedSourcesDirectory().getAbsolutePath() );     }
Maven:artifact generation <modelVersion>4.0.0</modelVersion> <groupId>si.srcsi</groupId> <artifactId>diners-mapping-batch</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>diners-mapping-batch</name>
Maven: execution #!/bin/bash mvn exec:java -Dexec.mainClass= “si.src.diners.mapping.batch.IcusBatch" -Dexec.args="2 $1 $2 $3 $2" Mvn takes care of classpath and environment variables maven_screencast.avi

More Related Content

What's hot

Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 
Unit testing: unitils & dbmaintain
Unit testing: unitils & dbmaintainUnit testing: unitils & dbmaintain
Unit testing: unitils & dbmaintain
nevenfi
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
Andy Peterson
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
Peter Wilcsinszky
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
snopteck
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
Carles Farré
 
Devoxx 15 equals hashcode
Devoxx 15 equals hashcodeDevoxx 15 equals hashcode
Devoxx 15 equals hashcode
bleporini
 
A JCR View of the World - adaptTo() 2012 Berlin
A JCR View of the World - adaptTo() 2012 BerlinA JCR View of the World - adaptTo() 2012 Berlin
A JCR View of the World - adaptTo() 2012 Berlin
Alexander Klimetschek
 

What's hot (20)

XML-Free Programming
XML-Free ProgrammingXML-Free Programming
XML-Free Programming
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
Unit testing: unitils & dbmaintain
Unit testing: unitils & dbmaintainUnit testing: unitils & dbmaintain
Unit testing: unitils & dbmaintain
 
Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)Visage Android Hands-on Lab (OSCON)
Visage Android Hands-on Lab (OSCON)
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
 
What's new in Java EE 6
What's new in Java EE 6What's new in Java EE 6
What's new in Java EE 6
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
 
05 status-codes
05 status-codes05 status-codes
05 status-codes
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
 
Rule Engine & Drools
Rule Engine & DroolsRule Engine & Drools
Rule Engine & Drools
 
Devoxx 15 equals hashcode
Devoxx 15 equals hashcodeDevoxx 15 equals hashcode
Devoxx 15 equals hashcode
 
ORM Injection
ORM InjectionORM Injection
ORM Injection
 
Download It
Download ItDownload It
Download It
 
#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...
#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...
#11.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원학원,재직자/실업자교육학원,스프링교육,마이바...
 
A JCR View of the World - adaptTo() 2012 Berlin
A JCR View of the World - adaptTo() 2012 BerlinA JCR View of the World - adaptTo() 2012 Berlin
A JCR View of the World - adaptTo() 2012 Berlin
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
Teste de Integração com DbUnit e jIntegrity
Teste de Integração com DbUnit e jIntegrityTeste de Integração com DbUnit e jIntegrity
Teste de Integração com DbUnit e jIntegrity
 
Deadly Code! (seriously) Blocking &amp; Hyper Context Switching Pattern
Deadly Code! (seriously) Blocking &amp; Hyper Context Switching PatternDeadly Code! (seriously) Blocking &amp; Hyper Context Switching Pattern
Deadly Code! (seriously) Blocking &amp; Hyper Context Switching Pattern
 

Similar to Sqladria 2009 SRC

Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
PrinceGuru MS
 
Fifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 MinutesFifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 Minutes
glassfish
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
Sagar Nakul
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
 

Similar to Sqladria 2009 SRC (20)

Jdbc
JdbcJdbc
Jdbc
 
Java Unit Testing with Unitils
Java Unit Testing with UnitilsJava Unit Testing with Unitils
Java Unit Testing with Unitils
 
I Feel Pretty
I Feel PrettyI Feel Pretty
I Feel Pretty
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server Pages
 
Slice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed PersistenceSlice: OpenJPA for Distributed Persistence
Slice: OpenJPA for Distributed Persistence
 
Core Php Component Presentation
Core Php Component PresentationCore Php Component Presentation
Core Php Component Presentation
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
Fifty New Features of Java EE 7 in Fifty Minutes
Fifty New Features of Java EE 7 in Fifty MinutesFifty New Features of Java EE 7 in Fifty Minutes
Fifty New Features of Java EE 7 in Fifty Minutes
 
Fifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 MinutesFifty Features of Java EE 7 in 50 Minutes
Fifty Features of Java EE 7 in 50 Minutes
 
Jsp
JspJsp
Jsp
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
 

Sqladria 2009 SRC

  • 1. SQLJ for Developers Andrea Parrilli Oracle SQL Expert, Developer Mvn: thanks to Damjan Kumar
  • 2.
  • 3. Short SQLJ syntax introduction;
  • 5. SQLJ on DB2 using Data Studio Developer, setup, binding, automatic building with maven;
  • 6. Performance comparison between SQLJ, unoptimized JDBC, JDBC with PreparedStatements;
  • 7.
  • 8. It is specified by ISO/IEC in module SQL/OLB to support portability.
  • 9.
  • 10. The Generated Code sqlj.runtime.ConnectionContext __sJT_connCtx = ctx; if (__sJT_connCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX(); sqlj.runtime.ExecutionContext __sJT_execCtx = __sJT_connCtx.getExecutionContext(); if (__sJT_execCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX(); synchronized (__sJT_execCtx) { sqlj.runtime.profile.RTStatement __sJT_stmt = __sJT_execCtx.registerStatement(__sJT_connCtx, MAPLOG_SJProfileKeys.getKey(0), 2); try { __sJT_stmt.setString(1, msg); __sJT_execCtx.executeUpdate(); } finally { __sJT_execCtx.releaseStatement(); } }
  • 11.
  • 12. DQL, DML & DDL: supports the full syntax of the underlying database manager.
  • 13.
  • 14. SQLJ Syntax Iterators #sql [context] iterator <it_name> implements sqlj.runtime.ForUpdate, sqlj.runtime.Scrollable with(holdability=… sensitivity=… updateColumns=“…”) { <JavaDataType> <field_name>, … } The standard defines datatypes mappings between Java and SQL: ex. INTEGER is mapped to BigDecimal, not to BigInteger!
  • 15. Why Maven? Manages library dependencies; Manages library download and installation; Manages the build process: SQLJ translation of .sqlj files into .java; Binding .ser files to the database; Generation of artifacts (.jar, .war, …); Execution from command line (calling the main method with the right classpath); Maven is Open Source! Once configured allows for a greater productivity shortening deploy and setup times and it fully integrates with the versioning system in use.
  • 16. How do we manage library dependencies Pom.xml Manual install <dependency> <groupId>com.ibm.db2</groupId> <artifactId>db2jcc</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>2.5.6</version> </dependency> mvn install:install-file -Dfile=lib/db2jcc.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc -Dversion=1.0 -Dpackaging=jar -Dgeneratepom=true Not everything is in the standard repositories!
  • 17. Maven: SQLJ translation of .sqlj into .java We use a custom maven plugin (sqlj-maven-plugin) for translation and binding of SQLJ files. <build> <plugins> <plugin> <groupId>si.srcsi</groupId> <artifactId>sqlj-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>sqljex</goal> <goal>sqljcustomize</goal> </goals> <configuration> <sqljDirs> <sqljDir>src/main/java</sqljDir> </sqljDirs> <generatedSourcesDirectory>src/main/java</generatedSourcesDirectory> <generatedResourcesDirectory>src/main/java</generatedResourcesDirectory> <classpath>lib/framework.jar</classpath> <dbUrl>${dbUrl}</dbUrl> <dbUser>${dbUser}</dbUser> <dbPassword>${dbPassword}</dbPassword> <dbCollection>${dbCollection}</dbCollection> </configuration> </execution> </executions> </plugin> </plugins> </build>
  • 18. Maven: SQLJ translation the implementation public class SqljExMojo extends AbstractSqljMojo{ private File[] sqljFiles; private File[] sqljDirs; public void execute() { ... Set sqljFiles = getSqljFiles(); for ( Iterator i=sqljFiles.iterator(); i.hasNext(); ){ File file = (File) i.next(); generate( file ); } Resource resource = new Resource(); resource.setDirectory( getGeneratedResourcesDirectory().getAbsolutePath() ); mavenProject.addResource( resource ); mavenProject.addCompileSourceRoot( getGeneratedSourcesDirectory().getAbsolutePath() ); }
  • 19. Maven:artifact generation <modelVersion>4.0.0</modelVersion> <groupId>si.srcsi</groupId> <artifactId>diners-mapping-batch</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>diners-mapping-batch</name>
  • 20. Maven: execution #!/bin/bash mvn exec:java -Dexec.mainClass= “si.src.diners.mapping.batch.IcusBatch" -Dexec.args="2 $1 $2 $3 $2" Mvn takes care of classpath and environment variables maven_screencast.avi
  • 21. Data Studio Developer Screencasts IDE_SQLJsupport.avi IDE_sqljassist.avi IDE_SQLJ_errors.avi IDE_debug.avi Deleted from Presentation: too heavy
  • 22. Performance Test SQLJ – JDBC - PreparedStatement SQLJ: 16 ms / 102 ms ± 10 ms; JDBC: 31 ms / 78 ms ± 10 ms; JDBC with PreparedStatement: 16 ms / 58 ms ± 15 ms; Note: smaller is faster. First figures are on a small single valued query the latter are for a 1000 elements iterator This is coherent with the implementation!
  • 23. The measure int iterations = 100; long sqljTime = 0; long jdbcTime = 0; long jdbcPreparedTime = 0; long start = 0; String str; //SQLJ #sql [ctx] { select str into :str from sirius.x_testsqlj where id = 3}; start = (new Date()).getTime(); for(int i = 0; i < iterations; i++) { #sql [ctx] { select str into :str from sirius.x_testsqlj where id = :(i%6)}; } sqljTime = (new Date()).getTime() - start; start = (new Date()).getTime(); Statement stmt = con.createStatement(); for(int i = 0; i < iterations; i++) { ResultSet rs = stmt.executeQuery("select str from sirius.x_testsqlj where id = " + (i%6)); rs.next(); tr = rs.getString(1);} jdbcTime = (new Date()).getTime() - start; start = (new Date()).getTime(); PreparedStatement ps = con.prepareStatement("select str from sirius.x_testsqlj where id = ?"); for(int i = 0; i < iterations; i++) { ps.setInt(1, i%6); ResultSet rs = ps.executeQuery(); rs.next(); str = rs.getString(1); } ps.close(); jdbcPreparedTime = (new Date()).getTime() - start;
  • 24. Performance comparison: SQL PL vs. Java for each row in the source: set savepoint; for each destination entity: check destination's business constraints; insert into destination table; if anything is wrong write to message buffer; if there are warnings in the buffer then flush it to messages table and rollback to savepoint; else commit;
  • 25. Performance comparison: SQL PL vs. Java Surprise! Over 10 executions of both implementations, run over a set of test data: Java + SQLJ: 8s ± 1s; SQL PL: 15s ± 1.5s; SQL PL without error logging: 10s ± 1s;
  • 26. Q&A

Editor's Notes

  1. This refers to DB2 implementation!