-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Driver rather than DriverManager
- Loading branch information
Showing
3 changed files
with
88 additions
and
39 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
ebean-datasource/src/main/java/io/ebean/datasource/pool/ObtainDriver.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.ebean.datasource.pool; | ||
|
||
import java.sql.Driver; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
final class ObtainDriver { | ||
|
||
/** | ||
* Return the jdbc Driver to use. | ||
*/ | ||
static Driver driver(String driver, String url) { | ||
if (driver != null && !driver.isEmpty()) { | ||
return initDriver(driver); | ||
} | ||
try { | ||
return DriverManager.getDriver(url); | ||
} catch (SQLException e) { | ||
throw new IllegalStateException("Unable to obtain Driver", e); | ||
} | ||
} | ||
|
||
private static Driver initDriver(String driver) { | ||
Class<?> driverClass = initDriverClass(driver); | ||
try { | ||
return Driver.class.cast(driverClass.getConstructor().newInstance()); | ||
} catch (Throwable e) { | ||
throw new IllegalStateException("Problem loading Database Driver: " + driver, e); | ||
} | ||
} | ||
|
||
private static Class<?> initDriverClass(String driver) { | ||
try { | ||
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); | ||
if (contextLoader != null) { | ||
return Class.forName(driver, true, contextLoader); | ||
} else { | ||
return Class.forName(driver, true, ObtainDriver.class.getClassLoader()); | ||
} | ||
} catch (Throwable e) { | ||
throw new IllegalStateException("Problem loading Database Driver: " + driver, e); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
ebean-datasource/src/test/java/io/ebean/datasource/pool/ObtainDriverTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.ebean.datasource.pool; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.Driver; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ObtainDriverTest { | ||
|
||
@Test | ||
void h2() { | ||
Driver h2Driver = ObtainDriver.driver(org.h2.Driver.class.getName(), "junk"); | ||
assertThat(h2Driver).isInstanceOf(org.h2.Driver.class); | ||
} | ||
|
||
@Test | ||
void h2Url() { | ||
assertThat(ObtainDriver.driver(null, "jdbc:h2:mem")).isInstanceOf(org.h2.Driver.class); | ||
assertThat(ObtainDriver.driver("", "jdbc:h2:mem")).isInstanceOf(org.h2.Driver.class); | ||
} | ||
|
||
@Test | ||
void postgres() { | ||
Driver h2Driver = ObtainDriver.driver(org.postgresql.Driver.class.getName(), "junk"); | ||
assertThat(h2Driver).isInstanceOf(org.postgresql.Driver.class); | ||
} | ||
|
||
@Test | ||
void postgresUrl() { | ||
Driver h2Driver = ObtainDriver.driver(null, "jdbc:postgresql://127.0.0.1:9999/app"); | ||
assertThat(h2Driver).isInstanceOf(org.postgresql.Driver.class); | ||
} | ||
} |