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

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...

Load testing MongoDB using JMeter

Apache JMeter ( http://jmeter.apache.org/ ) added support for MongoDB since its 2.10 release. In this post I am referring to the latest JMeter release (2.13). A preliminary JMeter setup is needed before starting your first test plan for MongoDB. It uses Groovy as scripting reference language, so Groovy needs to be set up for our favorite load testing tool. Follow these steps to complete the set up: Download Groovy from the official website ( http://www.groovy-lang.org/download.html ). In this post I am referring to the Groovy release 2.4.4, but using later versions is fine. Copy the groovy-all-2.4.4.jar to the $JMETER_HOME/lib folder. Restart JMeter if it was running while adding the Groovy JAR file. Now you can start creating a test plan for MongoDB load testing. From the UI select the MongoDB template ( File -> Templates... ). The new test plan has a MongoDB Source Config element. Here you have to setup the connection details for the database to be tested: The Threa...

Evaluating Pinpoint APM (Part 1)

I started a journey evaluating Open Source alternatives to commercial New Relic and AppDynamics tools to check if some is really ready to be used in a production environment. One cross-platform Application Performance Management (APM) tool that particularly caught my attention is Pinpoint . The current release supports mostly Java applications and JEE application servers and provides support also for the most popular OS and commercial relational databases. APIs are available to implement new plugins to support specific systems. Pinpoint has been modeled after Google Dapper and promises to install agents without changing a single line of code and mininal impact (about 3% increase in resource usage) on applications performance. Pinpoint is licensed under the Apache License, Version 2.0 . Architecture Pinpoint has three main components:  - The collector: it receives monitoring data from the profiled applications. It stores those information in HBase .  - The web UI: the f...