Created
October 17, 2012 01:55
-
-
Save azell/3903290 to your computer and use it in GitHub Desktop.
Minimal Spring / jOOQ transaction integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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