Giter VIP home page Giter VIP logo

jooq-javaee-example's Introduction

jOOQ JavaEE example

This example shows basic configuration for jOOQ on a JavaEE 7 compliant application server (was tested with Wildfly 8.x). The example focuses on few elements, handy when implementing a non JPA based "database handling" libraries

Resource producers

An annotation @javax.enterprise.inject.Produces is used to prepare the DSLContext object (with a desired datasource and database dialect). The produced beans becomes injectable within an application as any other CDI bean (since CDI 1.0)

  @Inject
  private DSLContext ctx;

Transaction handling

While jOOQ does not provide an API for transaction handling, it takes any DataSource (no matter how produced). Hence, if transaction can be provided externally (outside of jOOQ) a certain level of coupling between SQL statements can be provided. In this example we are using @Transactional annotation (since JTA 1.2).

The javax.transaction.Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.

This support is provided via an implementation of CDI interceptors that conduct the necessary suspending, resuming, etc.

In case of older Application Servers (JavaEE 6), a more verbose approach is required, using a lower level UserTransaction API

@Resource
UserTransaction tx

public void transactionalOperation() {}
  try {
   tx.begin();

   //some operations on DSLContext

   tx.commit();
  } catch (HeuristicRollbackException |
           RollbackException |
           NotSupportedException |
           HeuristicMixedException e) {
   tx.rollback();
   throw new RuntimeException(e);
  }
}

Application server datasource configuration

MySQL was used as a database for this example. Wildfly datasource configuration may look as follows:

<datasources>
  <datasource
    jndi-name="java:jboss/datasources/JooqExampleDS"
    pool-name="JooqExampleDS"
    enabled="true"
    use-java-context="true">
    <connection-url>
      jdbc:mysql://localhost/jooq
    </connection-url>
    <driver>mysql</driver>
    <security>
      <user-name>username</user-name>
      <password>password</password>
    </security>
  </datasource>
  <drivers>
    <driver name="mysql" module="com.mysql"> // (1)
      <driver-class>com.mysql.jdbc.Driver</driver-class>
    </driver>
  </drivers>
</datasources>
  1. The module needs to be defined externally - see Wildfly blog for reference - Creating a Module section

TODO

Some additional 'feature' will be added to make the example more complete:

  • โ PUT REST method to show resource update with jOOQ

  • โ example test, that verifies the transactin handling (and rollback)

jooq-javaee-example's People

Contributors

kubamarchwicki avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.