mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-04 21:01:21 +08:00
Translate SQLException thrown during connection acquisition
This commit is contained in:
parent
964ab8e08b
commit
b2ef8d7b0d
@ -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);
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user