Skip to content

Instantly share code, notes, and snippets.

@azell
Created October 17, 2012 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azell/3903290 to your computer and use it in GitHub Desktop.
Save azell/3903290 to your computer and use it in GitHub Desktop.
Minimal Spring / jOOQ transaction integration
import javax.sql.DataSource;
import org.jooq.ExecuteContext;
import org.jooq.impl.DefaultExecuteListener;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
/**
* This class converts between native SQL and Spring exceptions.
*
* @author $author$
* @version $Revision$, $Date$
*/
public class SpringExceptionTranslationExecuteListener
extends DefaultExecuteListener
{
/** {@inheritDoc} */
@Override
public void exception(ExecuteContext ctx)
{
ctx.exception(
getExceptionTranslator(ctx.getDataSource()).translate(
"jOOQ", ctx.sql(), ctx.sqlException()
)
);
}
/** {@inheritDoc} */
@Override
public void start(ExecuteContext ctx)
{
ctx.setConnection(DataSourceUtils.getConnection(ctx.getDataSource()));
}
/**
* Returns the value of the exception translator property.
*
* @param ds the data source.
*
* @return the value of the exception translator property.
*/
protected SQLExceptionTranslator getExceptionTranslator(DataSource ds)
{
return (ds != null) ? new SQLErrorCodeSQLExceptionTranslator(ds)
: new SQLStateSQLExceptionTranslator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment