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

Streamsets Data Collector log shipping and analysis using ElasticSearch, Kibana and... the Streamsets Data Collector

One common use case scenario for the Streamsets Data Collector (SDC) is the log shipping to some system, like ElasticSearch, for real-time analysis. To build a pipeline for this particular purpose in SDC is really simple and fast and doesn't require coding at all. For this quick tutorial I will use the SDC logs as example. The log data will be shipped to Elasticsearch and then visualized through a Kibana dashboard. Basic knowledge of SDC, Elasticsearch and Kibana is required for a better understanding of this post. These are the releases I am referring to for each system involved in this tutorial: JDK 8 Streamsets Data Collector 1.4.0 ElasticSearch 2.3.3 Kibana 4.5.1 Elasticsearch and Kibana installation You should have your Elasticsearch cluster installed and configured and a Kibana instance pointing to that cluster in order to go on with this tutorial. Please refer to the official documentation for these two products in order to complete their installation (if you do

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

Using Rapids cuDF in a Colab notebook

During last Spark+AI Summit Europe 2019 I had a chance to attend a talk from Miguel Martinez  who was presenting Rapids , the new Open Source framework from NVIDIA for GPU accelerated end-to-end Data Science and Analytics. Fig. 1 - Overview of the Rapids eco-system Rapids is a suite of Open Source libraries: cuDF cuML cuGraph cuXFilter I enjoied the presentation and liked the idea of this initiative, so I wanted to start playing with the Rapids libraries in Python on Colab , starting from cuDF, but the first attempt came with an issue that I eventually solved. So in this post I am going to share how I fixed it, with the hope it would be useful to someone else running into the same blocker. I am assuming here you are already familiar with Google Colab. I am using Python 3.x as Python 2 isn't supported by Rapids. Once you have created a new notebook in Colab, you need to check if the runtime for it is set to use Python 3 and uses a GPU as hardware accelerator. You