Tighten up transaction creation code to flag situations where calling code SHOULD BE within a demarcated transaction boundary. (#869)

Now fail fast with an appropriate message indicating course of action to be taken.
This commit is contained in:
josecoll 2017-06-20 20:33:31 +01:00 committed by GitHub
parent d54f66ccb0
commit 8aa325d9f7
3 changed files with 56 additions and 48 deletions

View File

@ -131,7 +131,7 @@ class KotlinConfigurationTransactionWrapper(private val model: EntityModel,
override fun getConnection(): Connection { override fun getConnection(): Connection {
val tx = TransactionManager.manager.currentOrNull() val tx = TransactionManager.manager.currentOrNull()
return CordaConnection( return CordaConnection(
tx?.connection ?: TransactionManager.manager.newTransaction(Connection.TRANSACTION_REPEATABLE_READ).connection tx?.connection ?: throw IllegalStateException("Was expecting to find database transaction: must wrap calling code within a transaction.")
) )
} }
} }

View File

@ -48,7 +48,7 @@ class RequeryConfiguration(val properties: Properties, val useDefaultLogging: Bo
// TODO: remove once Requery supports QUERY WITH COMPOSITE_KEY IN // TODO: remove once Requery supports QUERY WITH COMPOSITE_KEY IN
fun jdbcSession(): Connection { fun jdbcSession(): Connection {
val ctx = TransactionManager.manager.currentOrNull() val ctx = TransactionManager.manager.currentOrNull()
return ctx?.connection ?: TransactionManager.manager.newTransaction(Connection.TRANSACTION_REPEATABLE_READ).connection return ctx?.connection ?: throw IllegalStateException("Was expecting to find database transaction: must wrap calling code within a transaction.")
} }
} }

View File

@ -14,9 +14,9 @@ import net.corda.node.services.database.RequeryConfiguration
import net.corda.node.services.persistence.schemas.AttachmentEntity import net.corda.node.services.persistence.schemas.AttachmentEntity
import net.corda.node.services.transactions.PersistentUniquenessProvider import net.corda.node.services.transactions.PersistentUniquenessProvider
import net.corda.node.utilities.configureDatabase import net.corda.node.utilities.configureDatabase
import net.corda.node.utilities.transaction
import net.corda.testing.node.makeTestDataSourceProperties import net.corda.testing.node.makeTestDataSourceProperties
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -55,7 +55,7 @@ class NodeAttachmentStorageTest {
@After @After
fun tearDown() { fun tearDown() {
TransactionManager.current().close() dataSource.close()
} }
@Test @Test
@ -63,6 +63,7 @@ class NodeAttachmentStorageTest {
val testJar = makeTestJar() val testJar = makeTestJar()
val expectedHash = testJar.readAll().sha256() val expectedHash = testJar.readAll().sha256()
database.transaction {
val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry()) val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry())
val id = testJar.read { storage.importAttachment(it) } val id = testJar.read { storage.importAttachment(it) }
assertEquals(expectedHash, id) assertEquals(expectedHash, id)
@ -83,10 +84,12 @@ class NodeAttachmentStorageTest {
it.readBytes() it.readBytes()
} }
} }
}
@Test @Test
fun `duplicates not allowed`() { fun `duplicates not allowed`() {
val testJar = makeTestJar() val testJar = makeTestJar()
database.transaction {
val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry()) val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry())
testJar.read { testJar.read {
storage.importAttachment(it) storage.importAttachment(it)
@ -97,10 +100,12 @@ class NodeAttachmentStorageTest {
} }
} }
} }
}
@Test @Test
fun `corrupt entry throws exception`() { fun `corrupt entry throws exception`() {
val testJar = makeTestJar() val testJar = makeTestJar()
database.transaction {
val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry()) val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry())
val id = testJar.read { storage.importAttachment(it) } val id = testJar.read { storage.importAttachment(it) }
@ -124,9 +129,11 @@ class NodeAttachmentStorageTest {
it.readBytes() it.readBytes()
} }
} }
}
@Test @Test
fun `non jar rejected`() { fun `non jar rejected`() {
database.transaction {
val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry()) val storage = NodeAttachmentService(fs.getPath("/"), dataSourceProperties, MetricRegistry())
val path = fs.getPath("notajar") val path = fs.getPath("notajar")
path.writeLines(listOf("Hey", "there!")) path.writeLines(listOf("Hey", "there!"))
@ -136,6 +143,7 @@ class NodeAttachmentStorageTest {
} }
} }
} }
}
private var counter = 0 private var counter = 0
private fun makeTestJar(): Path { private fun makeTestJar(): Path {