Translate SQLException thrown during connection acquisition

This commit is contained in:
Chris Ribble 2018-05-25 10:29:59 -05:00 committed by Jan-Willem Gmelig Meyling
parent 964ab8e08b
commit b2ef8d7b0d
2 changed files with 20 additions and 3 deletions

View File

@ -34,9 +34,11 @@ public class SQLQueryFactory extends AbstractSQLQueryFactory<SQLQuery<?>> {
static class DataSourceProvider implements Provider<Connection> {
private final DataSource ds;
private final Configuration configuration;
public DataSourceProvider(DataSource ds) {
public DataSourceProvider(DataSource ds, Configuration configuration) {
this.ds = ds;
this.configuration = configuration;
}
@Override
@ -44,7 +46,7 @@ public class SQLQueryFactory extends AbstractSQLQueryFactory<SQLQuery<?>> {
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
throw configuration.translate(e);
}
}
@ -63,7 +65,7 @@ public class SQLQueryFactory extends AbstractSQLQueryFactory<SQLQuery<?>> {
}
public SQLQueryFactory(Configuration configuration, DataSource dataSource, boolean release) {
super(configuration, new DataSourceProvider(dataSource));
super(configuration, new DataSourceProvider(dataSource, configuration));
if (release) {
configuration.addListener(SQLCloseListener.DEFAULT);
}

View File

@ -16,13 +16,17 @@ package com.querydsl.sql;
import static org.junit.Assert.assertNotNull;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransientConnectionException;
import javax.inject.Provider;
import javax.sql.DataSource;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import com.querydsl.core.QueryException;
import com.querydsl.sql.domain.QSurvey;
public class SQLQueryFactoryTest {
@ -70,4 +74,15 @@ public class SQLQueryFactoryTest {
assertNotNull(queryFactory.merge(QSurvey.survey));
}
@Test(expected = QueryException.class)
public void dataSourceProviderException() throws SQLException {
SQLException e = new SQLTransientConnectionException();
DataSource ds = EasyMock.createStrictMock(DataSource.class);
EasyMock.expect(ds.getConnection()).andThrow(e);
EasyMock.replay(ds);
SQLQueryFactory exceptionQueryFactory = new SQLQueryFactory(new Configuration(SQLTemplates.DEFAULT), ds);
exceptionQueryFactory.getConnection();
}
}