Skip to content

Commit

Permalink
Use try-with-resources for JDBC AutoCloseable resources
Browse files Browse the repository at this point in the history
Refactored code to utilize try-with-resources for JDBC operations, ensuring
    automatic resource management (ARM) compliance. This change improves code
    readability and reliability by eliminating the need for explicit resource
    closure, reducing the risk of resource leaks.
  • Loading branch information
dixitdeepak committed Oct 28, 2024
1 parent edcd031 commit 14844df
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 160 deletions.
12 changes: 5 additions & 7 deletions framework/service/org/moqui/impl/InstanceServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,14 @@ along with this software (see the LICENSE.md file). If not, see
dbUserExists = false
javax.sql.XAConnection testXaCon = null
java.sql.Connection testCon = null
try {
testXaCon = ec.entity.getConfConnection(adminMap)
testCon = testXaCon.getConnection()
dbConnectSuccess = true
try (java.sql.Connection testCon = testXaCon.getConnection()) {
dbConnectSuccess = true
}
} catch (Exception e) {
logger.warn("Test connection failed", e)
} finally {
if (testCon != null) testCon.close()
if (testXaCon != null) testXaCon.close()
}
Expand Down Expand Up @@ -804,15 +803,14 @@ along with this software (see the LICENSE.md file). If not, see
dbUserExists = false
javax.sql.XAConnection testXaCon = null
java.sql.Connection testCon = null
try {
testXaCon = ec.entity.getConfConnection(adminMap)
testCon = testXaCon.getConnection()
try (java.sql.Connection testCon = testXaCon.getConnection()) {
dbConnectSuccess = true
}
} catch (Exception e) {
logger.warn("Test connection to Postgres failed", e)
} finally {
if (testCon != null) testCon.close()
if (testXaCon != null) testXaCon.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ class TransactionCache implements Synchronization {
}

void flushCache(boolean clearRead) {
Map<String, Connection> connectionByGroup = new HashMap<>()
try {
int writeInfoListSize = writeInfoList.size()
if (writeInfoListSize > 0) {
Expand All @@ -456,12 +455,7 @@ class TransactionCache implements Synchronization {
for (int i = 0; i < writeInfoListSize; i++) {
EntityWriteInfo ewi = (EntityWriteInfo) writeInfoList.get(i)
String groupName = ewi.evb.getEntityDefinition().getEntityGroupName()
Connection con = connectionByGroup.get(groupName)
if (con == null) {
con = efi.getConnection(groupName)
connectionByGroup.put(groupName, con)
}

try (Connection con = efi.getConnection(groupName)) {
if (ewi.writeMode.is(WriteMode.CREATE)) {
ewi.evb.basicCreate(con)
createCount++
Expand All @@ -472,6 +466,7 @@ class TransactionCache implements Synchronization {
ewi.evb.basicUpdate(con)
updateCount++
}
}
}
if (logger.isDebugEnabled()) logger.debug("Flushed TransactionCache in ${System.currentTimeMillis() - startTime}ms: ${createCount} creates, ${updateCount} updates, ${deleteCount} deletes, ${readOneCache.size()} read entries, ${readListCache.size()} entities with list cache")
}
Expand All @@ -489,9 +484,6 @@ class TransactionCache implements Synchronization {
} catch (Throwable t) {
logger.error("Error writing values from TransactionCache: ${t.toString()}", t)
throw new XAException("Error writing values from TransactionCache: + ${t.toString()}")
} finally {
// now close connections
for (Connection con in connectionByGroup.values()) con.close()
}
}

Expand Down
Loading

0 comments on commit 14844df

Please sign in to comment.