Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Attempting to integrate Spring Transaction with jOOQ 3

View Spring Txn and jOOQ 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
public class SpringExceptionTranslationExecuteListener
extends DefaultExecuteListener {
 
/** {@inheritDoc} */
@Override
public void exception(ExecuteContext ctx) {
SQLException e = ctx.sqlException();
 
if (e != null) {
String name = productName(ctx.configuration().dialect());
 
/* Prefer product name, if available. */
SQLExceptionTranslator translator = (name != null)
? new SQLErrorCodeSQLExceptionTranslator(name)
: new SQLStateSQLExceptionTranslator();
 
ctx.exception(translator.translate("jOOQ", ctx.sql(), e));
}
}
 
protected String productName(SQLDialect dialect) {
String name = null;
 
if (dialect == SQLDialect.HSQLDB) {
name = "HSQL";
} else if (dialect == SQLDialect.POSTGRES) {
name = "PostgreSQL";
} else if (dialect == SQLDialect.MARIADB) {
name = SQLDialect.MYSQL.getName();
} else if (dialect != null) {
name = dialect.getName();
}
 
return name;
}
}
 
public class SpringTransactionConnectionProvider implements ConnectionProvider {
private final DataSource ds;
 
public SpringTransactionConnectionProvider(DataSource ds) {
this.ds = ds;
}
 
/** {@inheritDoc} */
@Override
public Connection acquire() {
try {
return DataSourceUtils.doGetConnection(ds);
} catch (SQLException e) {
throw new DataAccessException(
"Error getting connection from data source " + ds, e);
}
}
 
/** {@inheritDoc} */
@Override
public void release(Connection conn) {
try {
DataSourceUtils.doReleaseConnection(conn, ds);
} catch (SQLException e) {
throw new DataAccessException("Error closing connection " + conn, e);
}
}
}
 
public class JooqTransactionFactory {
private final Configuration config = new DefaultConfiguration();
 
public JooqTransactionFactory(DataSource ds, SQLDialect dialect) {
this(ds, dialect, new Settings().withRenderSchema(false));
}
 
public JooqTransactionFactory(DataSource ds, SQLDialect dialect,
Settings settings) {
config.set(new SpringTransactionConnectionProvider(ds)).set(dialect).set(
settings).set(
new DefaultExecuteListenerProvider(
new SpringExceptionTranslationExecuteListener()));
}
 
public DSLContext context() {
return DSL.using(config);
}
}

Visitors of this gist:

Please do also consider the jOOQ manual's official tutorial on how to integrate jOOQ with Spring:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.