From 6bc3ba48acb3b92348a3eb11b2ca3c108ae65fc2 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Sun, 19 Dec 2021 10:06:24 -0500 Subject: [PATCH] Expose the getConnection method for use in plugins Resolves #770 --- .../org/mybatis/generator/config/Context.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java index f1d5c8afc1..3a980e3cf4 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java @@ -475,7 +475,16 @@ public void generateFiles(ProgressCallback callback, .contextGenerateAdditionalKotlinFiles()); } - private Connection getConnection() throws SQLException { + /** + * This method creates a new JDBC connection from the values specified in the configuration file. + * If you call this method, then you are responsible + * for closing the connection (See {@link Context#closeConnection(Connection)}). If you do not + * close the connection, then there could be connection leaks. + * + * @return a new connection created from the values in the configuration file + * @throws SQLException if any error occurs while creating the connection + */ + public Connection getConnection() throws SQLException { ConnectionFactory connectionFactory; if (jdbcConnectionConfiguration != null) { connectionFactory = new JDBCConnectionFactory(jdbcConnectionConfiguration); @@ -486,7 +495,13 @@ private Connection getConnection() throws SQLException { return connectionFactory.getConnection(); } - private void closeConnection(Connection connection) { + /** + * This method closes a JDBC connection and ignores any errors. If the passed connection is null, + * then the method does nothing. + * + * @param connection a JDBC connection to close, may be null + */ + public void closeConnection(Connection connection) { if (connection != null) { try { connection.close();