From 40edc286f56d1f4d10d791f6e0d4888d4835f805 Mon Sep 17 00:00:00 2001 From: Christian Sailer Date: Tue, 9 Jan 2018 17:17:10 +0000 Subject: [PATCH] Transaction creation performance (#303) (#320) * Default autoCommit to false, remove setting of autocommit and only set isolationLevel if it's changed. * Set default transaction isolation from database config --- .../corda/nodeapi/internal/persistence/CordaPersistence.kt | 3 ++- .../nodeapi/internal/persistence/DatabaseTransaction.kt | 6 ++++-- .../net/corda/node/services/config/NodeConfiguration.kt | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/CordaPersistence.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/CordaPersistence.kt index c8cc4cfc35..5289bdf302 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/CordaPersistence.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/CordaPersistence.kt @@ -35,7 +35,8 @@ enum class TransactionIsolationLevel { /** * The JDBC constant value of the same name but prefixed with TRANSACTION_ defined in [java.sql.Connection]. */ - val jdbcValue: Int = java.sql.Connection::class.java.getField("TRANSACTION_$name").get(null) as Int + val jdbcString = "TRANSACTION_$name" + val jdbcValue: Int = java.sql.Connection::class.java.getField(jdbcString).get(null) as Int } class CordaPersistence( diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/DatabaseTransaction.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/DatabaseTransaction.kt index 626a5433b4..4bb1bbd42e 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/DatabaseTransaction.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/DatabaseTransaction.kt @@ -20,8 +20,10 @@ class DatabaseTransaction( cordaPersistence.dataSource.connection .apply { _connectionCreated = true - autoCommit = false - transactionIsolation = isolation + // only set the transaction isolation level if it's actually changed - setting isn't free. + if (transactionIsolation != isolation) { + transactionIsolation = isolation + } } } diff --git a/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt b/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt index d843193f5c..9e7017eb0a 100644 --- a/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt +++ b/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt @@ -135,6 +135,13 @@ data class NodeConfigurationImpl( require(security == null || rpcUsers.isEmpty()) { "Cannot specify both 'rpcUsers' and 'security' in configuration" } + + // ensure our datasource configuration is sane + require(dataSourceProperties.get("autoCommit") != true) { "Datbase auto commit cannot be enabled, Corda requires transactional behaviour" } + dataSourceProperties.set("autoCommit", false) + if (dataSourceProperties.get("transactionIsolation") == null) { + dataSourceProperties["transactionIsolation"] = database.transactionIsolationLevel.jdbcString + } } }