PersistentNetworkMapCache close database connection (#1398)

This commit is contained in:
Katarzyna Streich 2017-09-04 09:41:00 +01:00 committed by Rick Parker
parent 39e44382be
commit e1363c91cc

View File

@ -248,21 +248,22 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
private fun updateInfoDB(nodeInfo: NodeInfo) {
// TODO Temporary workaround to force isolated transaction (otherwise it causes race conditions when processing
// network map registration on network map node)
val session = serviceHub.database.entityManagerFactory.withOptions().connection(serviceHub.database.dataSource.connection
.apply {
transactionIsolation = 1
}).openSession()
val tx = session.beginTransaction()
// TODO For now the main legal identity is left in NodeInfo, this should be set comparision/come up with index for NodeInfo?
val info = findByIdentityKey(session, nodeInfo.legalIdentity.owningKey)
val nodeInfoEntry = generateMappedObject(nodeInfo)
if (info.isNotEmpty()) {
nodeInfoEntry.id = info[0].id
serviceHub.database.dataSource.connection.use {
val session = serviceHub.database.entityManagerFactory.withOptions().connection(it.apply {
transactionIsolation = 1
}).openSession()
session.use {
val tx = session.beginTransaction()
// TODO For now the main legal identity is left in NodeInfo, this should be set comparision/come up with index for NodeInfo?
val info = findByIdentityKey(session, nodeInfo.legalIdentity.owningKey)
val nodeInfoEntry = generateMappedObject(nodeInfo)
if (info.isNotEmpty()) {
nodeInfoEntry.id = info[0].id
}
session.merge(nodeInfoEntry)
tx.commit()
}
}
session.merge(nodeInfoEntry)
tx.commit()
session.close()
}
private fun removeInfoDB(nodeInfo: NodeInfo) {