matrei commented on code in PR #15453:
URL: https://github.com/apache/grails-core/pull/15453#discussion_r2853501641
##########
grails-testing-support-dbcleanup-postgresql/src/main/groovy/org/apache/grails/testing/cleanup/postgresql/PostgresDatabaseCleanupHelper.groovy:
##########
@@ -16,67 +16,52 @@
* specific language governing permissions and limitations
* under the License.
*/
-
package org.apache.grails.testing.cleanup.postgresql
-import java.sql.Connection
-import java.sql.DatabaseMetaData
-
import javax.sql.DataSource
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
/**
- * Helper utility for PostgreSQL database cleanup operations. Provides
PostgreSQL-specific logic such as
- * resolving the current schema from a {@link DataSource} by inspecting JDBC
connection metadata
- * and parsing PostgreSQL JDBC URLs.
+ * Helper utility for PostgreSQL database cleanup operations.
+ * Provides PostgreSQL-specific logic such as resolving the
+ * current schema from a {@link DataSource} by inspecting
+ * JDBC connection metadata and parsing PostgreSQL JDBC URLs.
*/
@Slf4j
@CompileStatic
class PostgresDatabaseCleanupHelper {
/**
- * Resolves the current schema for the given PostgreSQL datasource by
inspecting the JDBC connection metadata.
- * If the JDBC URL contains a `currentSchema` parameter, returns that
schema.
+ * Resolves the current schema for the given PostgreSQL datasource
+ * by inspecting the JDBC connection metadata. If the JDBC URL
+ * contains a `currentSchema` parameter, returns that schema.
* Otherwise, returns the connection's current schema.
*
* @param dataSource the datasource to resolve the schema for
* @return the schema name, or {@code null} if it cannot be determined
*/
static String resolveCurrentSchema(DataSource dataSource) {
- Connection connection = null
- try {
- connection = dataSource.getConnection()
- String schema = connection.getSchema()
+ try (def con = dataSource.connection) {
+ def schema = con.getSchema()
if (schema) {
log.debug('Resolved current schema from connection: {}',
schema)
return schema
}
- // Fallback: try to get the schema from the database metadata URL
- DatabaseMetaData metaData = connection.getMetaData()
- String url = metaData.getURL()
+ def url = con.metaData.URL
if (url) {
schema = extractCurrentSchemaFromUrl(url)
- if (schema) {
- log.debug('Resolved current schema from URL {}: {}', url,
schema)
- return schema
- }
- }
- }
- finally {
- if (connection) {
- try {
- connection.close()
- }
- catch (Exception ignored) {
- // ignore
- }
+ log.debug('Resolved current schema from URL {}: {}', url,
schema)
+ return schema
}
}
-
- throw new IllegalStateException("Because postgres defaults to the
search_path when currentSchema isn't defined, a schema should always be found")
+ throw new IllegalStateException(
+ 'Because postgres defaults to the search_path ' +
Review Comment:
Updated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]