Skip to main content

Using jOOQ with Spring

Today a brief tutorial on how to integrate jOOQ (http://www.jooq.org/) with Spring.
It covers the basic integration that could be easily applied to any web application.
For this tutorial I am referring to Spring 3.2.0 and jOOQ 2.6.1.
I'm referring to Oracle because the applications I am working on use this database, but all the considerations done here are valid for each database vendor supported by jOOQ.

Suppose you have a web application Spring based and want to integrate jOOQ with the most popular Inversion of Control container. First of all you have to put the jOOQ libraries in the WEB-INF/lib folder of  your application. The required jOOQ libraries are the following:

  • jooq-2.6.1.jar
  • jooq-codegen-2.6.1.jar
  • jooq-meta-2.6.1.jar
If you use the jOOQ code generation feature you need to add to the classpath also the jooq-codegen-2.6.1.jar library.
Then you have to put in the WEB-INF/lib folder also the jOOQ dependencies:


  • log4j-1.2.16.jar
  • persistence-api-1.0.jar
  • slf4j-api-1.6.1.jar
  • validation-api-1.1.0.Alpha1.jar


The configuration has to be done in the application context file. First you have to set the datasource


<bean id="appDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@dbhost:1700:DBSID"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>


<bean id="datasourceConnection" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" 
      lazy-init="true" depends-on="appDataSource">
        <property name="targetObject">
            <ref bean="appDataSource"/>
        </property>
        <property name="targetMethod">
            <value>getConnection</value>
        </property>
    </bean>

Then you have to extend the jOOQ default Factory (org.jooq.impl.Factory) this way:


public class CustomFactory extends Factory {
private static final long serialVersionUID = -4943523198068931839L;

public CustomFactory (Connection connection, SQLDialect dialect) {
super(connection, dialect);
}

}


and register it as bean in the application context file:


<bean id="customFactory" class="jooqspring.dataaccess.CustomFactory" lazy-init="true"
      depends-on="datasourceConnection" scope="prototype">
        <constructor-arg index="0" ref="datasourceConnection"  />
        <constructor-arg index="1" type="org.jooq.SQLDialect" value="ORACLE" />
</bean>


passing the datasource connection already defined and the jOOQ SQLDialect type (changing database vendor you just need to change the SQLDialect value here) as arguments for the overridden constructor. Now you can implement your class that builds and execute queries through jOOQ. A simple example (Java implementation + bean registration) :


public class ApplicationDataAccess {
private CustomFactory customFactory;

public void setCustomFactory(CustomFactory customFactory) {
this.customFactorycustomFactory;
}

public void getApplications() {
Result<Record> result = customFactory.select().from(Tables.APPLICATION).fetch();
                // Do something with the results...
}
}



<bean id="applicationDataAccess" class="jooqspring.dataaccess.ApplicationDataAccess">
<property name="customFactory" ref="customFactory"/>
</bean>


and its usage:


<bean id="applicationsController" class="jooqspring.controller.ApplicationsController">
<property name="applicationDataAccess" ref="applicationDataAccess"/>
</bean>


public class ApplicationsController extends AbstractController {
private ApplicationDataAccess applicationDataAccess;

public void setApplicationDataAccess(ApplicationDataAccess applicationDataAccess) {
this.applicationDataAccess = applicationDataAccess;
}

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
...

applicationDataAccess.getApplications();

...
}

}



This is just a simple tutorial but it helps to understand how to quickly realize the integration between jOOQ and Spring. In the following weeks we will see how to make more complicated things ;)

Comments

  1. Thanks a lot for this writeup. For future visitors of this blog post:: Please also consider the official jOOQ/Spring integration tutorial:

    http://www.jooq.org/doc/latest/manual/getting-started/tutorials/jooq-with-spring/

    ReplyDelete

Post a Comment

Popular posts from this blog

jOOQ: code generation in Eclipse

jOOQ allows code generation from a database schema through ANT tasks, Maven and shell command tools. But if you're working with Eclipse it's easier to create a new Run Configuration to perform this operation. First of all you have to write the usual XML configuration file for the code generation starting from the database: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-2.0.4.xsd">   <jdbc>     <driver>oracle.jdbc.driver.OracleDriver</driver>     <url>jdbc:oracle:thin:@dbhost:1700:DBSID</url>     <user>DB_FTRS</user>     <password>password</password>   </jdbc>   <generator>     <name>org.jooq.util.DefaultGenerator</name>     <database>       <name>org.jooq.util.oracle.OracleDatabase</name>     ...

Exporting InfluxDB data to a CVS file

Sometimes you would need to export a sample of the data from an InfluxDB table to a CSV file (for example to allow a data scientist to do some offline analysis using a tool like Jupyter, Zeppelin or Spark Notebook). It is possible to perform this operation through the influx command line client. This is the general syntax: sudo /usr/bin/influx -database '<database_name>' -host '<hostname>' -username '<username>'  -password '<password>' -execute 'select_statement' -format '<format>' > <file_path>/<file_name>.csv where the format could be csv , json or column . Example: sudo /usr/bin/influx -database 'telegraf' -host 'localhost' -username 'admin'  -password '123456789' -execute 'select * from mem' -format 'csv' > /home/googlielmo/influxdb-export/mem-export.csv

Turning Python Scripts into Working Web Apps Quickly with Streamlit

 I just realized that I am using Streamlit since almost one year now, posted about in Twitter or LinkedIn several times, but never wrote a blog post about it before. Communication in Data Science and Machine Learning is the key. Being able to showcase work in progress and share results with the business makes the difference. Verbal and non-verbal communication skills are important. Having some tool that could support you in this kind of conversation with a mixed audience that couldn't have a technical background or would like to hear in terms of results and business value would be of great help. I found that Streamlit fits well this scenario. Streamlit is an Open Source (Apache License 2.0) Python framework that turns data or ML scripts into shareable web apps in minutes (no kidding). Python only: no front‑end experience required. To start with Streamlit, just install it through pip (it is available in Anaconda too): pip install streamlit and you are ready to execute the working de...