SQLJ for Developers<br />Andrea Parrilli<br />Oracle SQL Expert, Developer<br />Mvn: thanks to Damjan Kumar<br />
Topics<br /><ul><li>What is SQLJ;
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</li></li></ul><li>What is SQLJ<br /><ul><li>SQLJ is an extension of Java language ...
It is specified by ISO/IEC in module SQL/OLB to support portability.
The SQLJ preprocessor produces JDBC code with precompiled prepared statements.</li></li></ul><li>What is a Preprocessor?<b...
The Generated Code<br />sqlj.runtime.ConnectionContext __sJT_connCtx = ctx;<br />  if (__sJT_connCtx == null) 	sqlj.runtim...
SQLJ Syntax<br /><ul><li>ITERATORS: an iterator represents a result set or a cursor, it supports positional update/delete,...
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), th...
SQLJ Syntax Iterators<br />#sql [context] iterator <it_name> <br />	implements sqlj.runtime.ForUpdate,<br />		sqlj.runtime...
Why Maven?<br />Manages library dependencies;<br />Manages library download and installation;<br />Manages the build proce...
How do we manage library dependencies<br />Pom.xml<br />Manual install<br /><dependency><br />	<groupId>com.ibm.db2</group...
Maven: SQLJ translation of .sqlj into .java<br />We use a custom maven plugin (sqlj-maven-plugin) for translation and bind...
Maven: SQLJ translation the implementation<br />public class SqljExMojo extends AbstractSqljMojo{<br />    private File[] ...
Maven:artifact generation<br /><modelVersion>4.0.0</modelVersion><br /><groupId>si.srcsi</groupId><br /><artifactId>diners...
Maven: execution<br />#!/bin/bash<br />mvn exec:java<br />-Dexec.mainClass= “si.src.diners.mapping.batch.IcusBatch" -Dexec...
Upcoming SlideShare
Loading in …5
×

Sqladria 2009 SRC

861 views

Published on

SQLAdria 2009 presentation's slides about our experience with Maven and SQLJ. It delves in some details about SQLJ and performance comparion between SQLJ and JDBC with prepared statements. It also shows surprising results for what concerns performances of (DB2) SQL PL procedures

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
861
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • This refers to DB2 implementation!
  • Sqladria 2009 SRC

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

    ×