[#6982] Make jooq-meta's org.jooq.util.Database AutoCloseable

This commit is contained in:
lukaseder 2018-01-04 12:21:03 +01:00
parent e1969d9568
commit b12cbca32c
5 changed files with 36 additions and 2 deletions

View File

@ -245,6 +245,8 @@ public class GenerationTool {
if (g.getTarget() == null)
g.setTarget(new Target());
Database database = null;
try {
// Initialise connection
@ -306,7 +308,7 @@ public class GenerationTool {
: connection != null
? databaseClass(connection)
: databaseClass(j);
Database database = databaseClass.newInstance();
database = databaseClass.newInstance();
database.setProperties(properties(d.getProperties()));
List<Catalog> catalogs = d.getCatalogs();
@ -649,6 +651,13 @@ public class GenerationTool {
generator.generate(database);
}
finally {
if (database != null)
try {
database.close();
}
catch (Exception e) {
log.error("Error while closing database", e);
}
// Close connection only if it was created by the GenerationTool
if (close && connection != null)

View File

@ -57,6 +57,7 @@ import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.jooq.impl.ParserException;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jooq.util.SchemaDefinition;
import org.jooq.util.h2.H2Database;
@ -151,6 +152,12 @@ public class DDLDatabase extends H2Database {
return DSL.using(connection);
}
@Override
public void close() {
JDBCUtils.safeClose(connection);
super.close();
}
@Override
protected List<SchemaDefinition> getSchemata0() throws SQLException {
List<SchemaDefinition> result = new ArrayList<SchemaDefinition>(super.getSchemata0());

View File

@ -61,6 +61,7 @@ import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.jooq.impl.JPAConverter;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jooq.util.SchemaDefinition;
import org.jooq.util.h2.H2Database;
import org.jooq.util.jaxb.ForcedType;
@ -100,6 +101,12 @@ public class JPADatabase extends H2Database {
private Connection connection;
@Override
public void close() {
JDBCUtils.safeClose(connection);
super.close();
}
@Override
protected DSLContext create0() {
if (connection == null) {

View File

@ -2099,6 +2099,9 @@ public abstract class AbstractDatabase implements Database {
}
}
@Override
public void close() {}
/**
* Create a new Factory
*/

View File

@ -59,7 +59,7 @@ import org.jooq.util.jaxb.Schema;
*
* @author Lukas Eder
*/
public interface Database {
public interface Database extends AutoCloseable {
/**
* The catalogs generated from this database.
@ -782,6 +782,14 @@ public interface Database {
*/
Properties getProperties();
/**
* Release any resources that this Database may have allocated.
*/
@Override
void close();
/**
* A filter type that can be used with {@link Database#addFilter(Filter)}
*/