From cf27f62991dd3368b8f05b2b047285e5ab6df65a Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 14 Jun 2013 16:16:47 +0200 Subject: [PATCH] [#1836] Document using jOOQ with Spring for transaction support --- jOOQ-codegen-maven-example/pom.xml | 99 ++++++++- .../resources/jooq-spring-config-minimal.xml | 44 ++++ .../test/java/org/jooq/test/spring/Test.java | 73 +++++++ .../src/main/resources/manual-3.1.xml | 189 +++++++++++++++++- 4 files changed, 399 insertions(+), 6 deletions(-) create mode 100644 jOOQ-codegen-maven-example/src/main/resources/jooq-spring-config-minimal.xml create mode 100644 jOOQ-codegen-maven-example/src/test/java/org/jooq/test/spring/Test.java diff --git a/jOOQ-codegen-maven-example/pom.xml b/jOOQ-codegen-maven-example/pom.xml index df5a55a854..efe993cb8b 100644 --- a/jOOQ-codegen-maven-example/pom.xml +++ b/jOOQ-codegen-maven-example/pom.xml @@ -13,6 +13,11 @@ jooq-codegen-maven-example jOOQ Codegen Maven (Example) + + UTF-8 + 3.2.3.RELEASE + + log4j @@ -50,11 +55,97 @@ jar true - - - UTF-8 - + + commons-dbcp + commons-dbcp + 1.4 + + + + + org.springframework + spring-core + ${org.springframework.version} + + + + + org.springframework + spring-beans + ${org.springframework.version} + + + + + org.springframework + spring-context + ${org.springframework.version} + + + + + + + + + + + + + + + org.springframework + spring-jdbc + ${org.springframework.version} + + + + + + + + diff --git a/jOOQ-codegen-maven-example/src/main/resources/jooq-spring-config-minimal.xml b/jOOQ-codegen-maven-example/src/main/resources/jooq-spring-config-minimal.xml new file mode 100644 index 0000000000..fa585559dd --- /dev/null +++ b/jOOQ-codegen-maven-example/src/main/resources/jooq-spring-config-minimal.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + H2 + + + + \ No newline at end of file diff --git a/jOOQ-codegen-maven-example/src/test/java/org/jooq/test/spring/Test.java b/jOOQ-codegen-maven-example/src/test/java/org/jooq/test/spring/Test.java new file mode 100644 index 0000000000..6f0bb8e589 --- /dev/null +++ b/jOOQ-codegen-maven-example/src/test/java/org/jooq/test/spring/Test.java @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.test.spring; + +import org.jooq.DSLContext; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +public class Test { + public static void main(String[] args) { + + // Configure the Spring application context, loading the bean configuration + ApplicationContext context = + new ClassPathXmlApplicationContext(new String[] { + "jooq-spring-config-minimal.xml" + }); + + // Fetch a DSLContext reference + DSLContext dsl = context.getBean("dsl", DSLContext.class); + + // Execute a jOOQ query in its isolated "ad-hoc" transaction + // --------------------------------------------------------- + System.out.println(dsl.selectOne().fetch()); + + // Execute some jOOQ queries in an explicit transaction + // ---------------------------------------------------- + PlatformTransactionManager transactionManager = + context.getBean("transactionManager", PlatformTransactionManager.class); + + TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition()); + dsl.selectOne().fetch(); + dsl.selectOne().fetch(); + dsl.selectOne().fetch(); + transactionManager.commit(tx); + } +} diff --git a/jOOQ-website/src/main/resources/manual-3.1.xml b/jOOQ-website/src/main/resources/manual-3.1.xml index 628be0cf6b..832887710a 100644 --- a/jOOQ-website/src/main/resources/manual-3.1.xml +++ b/jOOQ-website/src/main/resources/manual-3.1.xml @@ -906,9 +906,194 @@ public class Main {
- Using jOOQ with Spring + Using jOOQ with Spring and DBCP -

Feel free to contribute a tutorial!

+

+ jOOQ and Spring are easy to integrate. In this example, we shall integrate: +

+ + +

+ The following steps show how to integrate the libraries. +

+ +

Add the required Maven dependencies

+

+ For this example, we'll create the following Maven dependencies +

+ + + + 3.2.3.RELEASE + + + + + org.jooq + jooq + {jooq-version} + + + com.h2database + h2 + 1.3.168 + + + commons-dbcp + commons-dbcp + 1.4 + + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-beans + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-jdbc + ${org.springframework.version} + + +]]> + +

Create a minimal Spring configuration file

+

+ The above dependencies are configured together using a Spring Beans configuration: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + H2 + + + +]]> + +

Run a query in the JDBC Connection's transaction:

+

+ The following simple program shows how you can now easily obtain a instance, from which queries can be executed: +

+ + + +

+ The above example shows how Spring's TransactionAwareDataSourceProxy discovers that a jOOQ query is not executed in a transactional context. +

+ +

Run a query in an "explicit transaction":

+

+ The following simple program shows how Spring's TransactionAwareDataSourceProxy will discover that several jOOQ queries are executed in the context of an explicitly created transaction +

+ + + +

+ Of course, in an actual productive setup, you are more likely to use Spring's AOP features to declare transactions and transactional behaviour on service methods, instead of explicitly starting and committing / rollbacking transactions. There are many other transaction models that you can choose to use with jOOQ. +