Ensure that ServiceHub.WithEntityManager has a database transaction available (#5413)

* tidy up withEntityManager

* address rick review comment

* api break
This commit is contained in:
Stefano Franz 2019-08-30 14:58:50 +00:00 committed by Roger Willis
parent 136600c91a
commit cd0d5c7724
4 changed files with 14 additions and 10 deletions

View File

@ -3520,7 +3520,6 @@ public interface net.corda.core.node.ServiceHub extends net.corda.core.node.Serv
@NotNull @NotNull
public abstract net.corda.core.contracts.StateAndRef<T> toStateAndRef(net.corda.core.contracts.StateRef) public abstract net.corda.core.contracts.StateAndRef<T> toStateAndRef(net.corda.core.contracts.StateRef)
public abstract void withEntityManager(java.util.function.Consumer<javax.persistence.EntityManager>) public abstract void withEntityManager(java.util.function.Consumer<javax.persistence.EntityManager>)
@NotNull
public abstract T withEntityManager(kotlin.jvm.functions.Function1<? super javax.persistence.EntityManager, ? extends T>) public abstract T withEntityManager(kotlin.jvm.functions.Function1<? super javax.persistence.EntityManager, ? extends T>)
## ##
@DoNotImplement @DoNotImplement
@ -7816,7 +7815,6 @@ public class net.corda.testing.node.MockServices extends java.lang.Object implem
@NotNull @NotNull
public net.corda.core.contracts.StateAndRef<T> toStateAndRef(net.corda.core.contracts.StateRef) public net.corda.core.contracts.StateAndRef<T> toStateAndRef(net.corda.core.contracts.StateRef)
public void withEntityManager(java.util.function.Consumer<javax.persistence.EntityManager>) public void withEntityManager(java.util.function.Consumer<javax.persistence.EntityManager>)
@NotNull
public T withEntityManager(kotlin.jvm.functions.Function1<? super javax.persistence.EntityManager, ? extends T>) public T withEntityManager(kotlin.jvm.functions.Function1<? super javax.persistence.EntityManager, ? extends T>)
public static final net.corda.testing.node.MockServices$Companion Companion public static final net.corda.testing.node.MockServices$Companion Companion
## ##

View File

@ -376,7 +376,7 @@ interface ServiceHub : ServicesForResolution {
* *
* @param block a lambda function with access to an [EntityManager]. * @param block a lambda function with access to an [EntityManager].
*/ */
fun <T : Any> withEntityManager(block: EntityManager.() -> T): T fun <T : Any?> withEntityManager(block: EntityManager.() -> T): T
/** /**
* Exposes the Java Persistence API (JPA) to flows via a restricted [EntityManager]. This method can be used to * Exposes the Java Persistence API (JPA) to flows via a restricted [EntityManager]. This method can be used to

View File

@ -409,7 +409,9 @@ abstract class AbstractNode<S>(val configuration: NodeConfiguration,
// Shut down the SMM so no Fibers are scheduled. // Shut down the SMM so no Fibers are scheduled.
runOnStop += { smm.stop(acceptableLiveFiberCountOnStop()) } runOnStop += { smm.stop(acceptableLiveFiberCountOnStop()) }
(smm as? StateMachineManagerInternal)?.let { (smm as? StateMachineManagerInternal)?.let {
val flowMonitor = FlowMonitor({ smm.snapshot().filter { flow -> flow !in smm.flowHospital }.toSet() }, configuration.flowMonitorPeriodMillis, configuration.flowMonitorSuspensionLoggingThresholdMillis) val flowMonitor = FlowMonitor({
smm.snapshot().filter { flow -> flow !in smm.flowHospital }.toSet()
}, configuration.flowMonitorPeriodMillis, configuration.flowMonitorSuspensionLoggingThresholdMillis)
runOnStop += flowMonitor::stop runOnStop += flowMonitor::stop
flowMonitor.start() flowMonitor.start()
} }
@ -596,7 +598,7 @@ abstract class AbstractNode<S>(val configuration: NodeConfiguration,
// Needed because of bug CORDA-2653 - some Corda services can utilise third-party libraries that require access to // Needed because of bug CORDA-2653 - some Corda services can utilise third-party libraries that require access to
// the Thread context class loader // the Thread context class loader
val oldContextClassLoader : ClassLoader? = Thread.currentThread().contextClassLoader val oldContextClassLoader: ClassLoader? = Thread.currentThread().contextClassLoader
try { try {
Thread.currentThread().contextClassLoader = cordappLoader.appClassLoader Thread.currentThread().contextClassLoader = cordappLoader.appClassLoader
@ -1032,12 +1034,16 @@ abstract class AbstractNode<S>(val configuration: NodeConfiguration,
override fun jdbcSession(): Connection = database.createSession() override fun jdbcSession(): Connection = database.createSession()
override fun <T : Any> withEntityManager(block: EntityManager.() -> T): T { override fun <T : Any?> withEntityManager(block: EntityManager.() -> T): T {
return block(contextTransaction.restrictedEntityManager) return database.transaction {
block(restrictedEntityManager)
}
} }
override fun withEntityManager(block: Consumer<EntityManager>) { override fun withEntityManager(block: Consumer<EntityManager>) {
block.accept(contextTransaction.restrictedEntityManager) withEntityManager {
block.accept(this)
}
} }
// allows services to register handlers to be informed when the node stop method is called // allows services to register handlers to be informed when the node stop method is called

View File

@ -224,7 +224,7 @@ open class MockServices private constructor(
override fun jdbcSession(): Connection = persistence.createSession() override fun jdbcSession(): Connection = persistence.createSession()
override fun <T : Any> withEntityManager(block: EntityManager.() -> T): T { override fun <T : Any?> withEntityManager(block: EntityManager.() -> T): T {
return block(contextTransaction.restrictedEntityManager) return block(contextTransaction.restrictedEntityManager)
} }
@ -440,7 +440,7 @@ open class MockServices private constructor(
override fun jdbcSession(): Connection = throw UnsupportedOperationException() override fun jdbcSession(): Connection = throw UnsupportedOperationException()
override fun <T : Any> withEntityManager(block: EntityManager.() -> T): T { override fun <T : Any?> withEntityManager(block: EntityManager.() -> T): T {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }