eu.drus.jpa.unit.sql.dbunit.DatabaseConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-unit-rdbms Show documentation
Show all versions of jpa-unit-rdbms Show documentation
Makes JPA Unit support SQL databases
package eu.drus.jpa.unit.sql.dbunit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ServiceLoader;
import org.apache.commons.dbcp2.BasicDataSource;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import eu.drus.jpa.unit.api.JpaUnitException;
import eu.drus.jpa.unit.sql.dbunit.ext.DbUnitConnectionFactory;
public final class DatabaseConnectionFactory {
private static final ServiceLoader SERVICE_LOADER = ServiceLoader.load(DbUnitConnectionFactory.class);
private DatabaseConnectionFactory() {}
public static IDatabaseConnection openConnection(final BasicDataSource ds) {
try {
final Connection connection = ds.getConnection();
for (final DbUnitConnectionFactory impl : SERVICE_LOADER) {
if (impl.supportsDriver(ds.getDriverClassName())) {
return impl.createConnection(connection);
}
}
// fall back if no specific implementation is available
return new DatabaseConnection(connection);
} catch (final DatabaseUnitException | SQLException e) {
throw new JpaUnitException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy