[#1341] Add DSL.using(String url) and other methods that mimick DriverManager.getConnection()
This commit is contained in:
parent
1027d8734a
commit
e541e9735e
@ -66,13 +66,16 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
import javax.sql.DataSource;
|
||||
@ -314,6 +317,69 @@ public class DSL {
|
||||
return new DefaultDSLContext(dialect, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an executor from a JDBC connection URL.
|
||||
* <p>
|
||||
* The connections created this way will be closed upon finalization. This
|
||||
* is useful for standalone scripts, but not for managed connections.
|
||||
*
|
||||
* @param url The connection URL.
|
||||
* @see DefaultConnectionProvider
|
||||
* @see JDBCUtils#dialect(String)
|
||||
*/
|
||||
public static DSLContext using(String url) {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(url);
|
||||
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw Utils.translate("Error when initialising Connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an executor from a JDBC connection URL.
|
||||
* <p>
|
||||
* The connections created this way will be closed upon finalization. This
|
||||
* is useful for standalone scripts, but not for managed connections.
|
||||
*
|
||||
* @param url The connection URL.
|
||||
* @param username The connection user name.
|
||||
* @param password The connection password.
|
||||
* @see DefaultConnectionProvider
|
||||
* @see JDBCUtils#dialect(String)
|
||||
*/
|
||||
public static DSLContext using(String url, String username, String password) {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw Utils.translate("Error when initialising Connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an executor from a JDBC connection URL.
|
||||
* <p>
|
||||
* The connections created this way will be closed upon finalization. This
|
||||
* is useful for standalone scripts, but not for managed connections.
|
||||
*
|
||||
* @param url The connection URL.
|
||||
* @param properties The connection properties.
|
||||
* @see DefaultConnectionProvider
|
||||
* @see JDBCUtils#dialect(String)
|
||||
*/
|
||||
public static DSLContext using(String url, Properties properties) {
|
||||
try {
|
||||
Connection connection = DriverManager.getConnection(url, properties);
|
||||
return using(new DefaultConnectionProvider(connection, true), JDBCUtils.dialect(connection));
|
||||
}
|
||||
catch (SQLException e) {
|
||||
throw Utils.translate("Error when initialising Connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an executor with a connection configured.
|
||||
* <p>
|
||||
|
||||
@ -47,6 +47,7 @@ import java.sql.Savepoint;
|
||||
import org.jooq.ConnectionProvider;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.jdbc.JDBCUtils;
|
||||
|
||||
/**
|
||||
* A default implementation for {@link ConnectionProvider}.
|
||||
@ -64,9 +65,15 @@ public class DefaultConnectionProvider implements ConnectionProvider {
|
||||
|
||||
private static final JooqLogger log = JooqLogger.getLogger(DefaultConnectionProvider.class);
|
||||
private Connection connection;
|
||||
private boolean finalize;
|
||||
|
||||
public DefaultConnectionProvider(Connection connection) {
|
||||
this(connection, false);
|
||||
}
|
||||
|
||||
DefaultConnectionProvider(Connection connection, boolean finalize) {
|
||||
this.connection = connection;
|
||||
this.finalize = finalize;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -81,6 +88,14 @@ public class DefaultConnectionProvider implements ConnectionProvider {
|
||||
@Override
|
||||
public final void release(Connection released) {}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
if (finalize)
|
||||
JDBCUtils.safeClose(connection);
|
||||
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX: Original DSLContext/Factory API (JDBC utility methods)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user