Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExposedTransactionManager added
- Loading branch information
Showing
12 changed files
with
457 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
rootProject.name='exposed' | ||
rootProject.name='exposed' | ||
include 'spring-transaction' | ||
|
61 changes: 61 additions & 0 deletions
61
spring-transaction/src/main/kotlin/org/jetbrains/exposed/spring/ExposedTransactionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.jetbrains.exposed.spring | ||
|
||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.Transaction | ||
import org.jetbrains.exposed.sql.TransactionAbstraction | ||
import org.jetbrains.exposed.sql.TransactionProvider | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager | ||
import org.springframework.jdbc.datasource.JdbcTransactionObjectSupport | ||
import org.springframework.transaction.TransactionDefinition | ||
import org.springframework.transaction.support.DefaultTransactionDefinition | ||
import org.springframework.transaction.support.DefaultTransactionStatus | ||
import org.springframework.transaction.support.TransactionSynchronizationManager | ||
import java.sql.Connection | ||
import javax.sql.DataSource | ||
|
||
|
||
class ExposedTransactionManager(dataSource: DataSource) : DataSourceTransactionManager(dataSource), TransactionProvider { | ||
|
||
private val db = Database.connect(dataSource, { this } ) | ||
|
||
override fun newTransaction(isolation: Int): Transaction { | ||
val tDefinition = if (dataSource.connection.transactionIsolation != isolation) { | ||
DefaultTransactionDefinition().apply { isolationLevel = isolation } | ||
} else null | ||
val tObject = (getTransaction(tDefinition) as DefaultTransactionStatus).transaction as JdbcTransactionObjectSupport | ||
return Transaction(SpringTransaction(tObject, db)).apply { | ||
TransactionSynchronizationManager.bindResource(this@ExposedTransactionManager, this) | ||
} | ||
} | ||
|
||
override fun close() { | ||
TransactionSynchronizationManager.unbindResourceIfPossible(this) | ||
} | ||
|
||
override fun currentOrNull(): Transaction? { | ||
return TransactionSynchronizationManager.getResource(this) as Transaction? ?: newTransaction(TransactionDefinition.ISOLATION_DEFAULT) | ||
} | ||
|
||
private class SpringTransaction(private val tObject: JdbcTransactionObjectSupport, override val db: Database) : TransactionAbstraction { | ||
|
||
override val connection: Connection get() = tObject.connectionHolder.connection | ||
|
||
override fun commit() { | ||
tObject.connectionHolder.connection.run { | ||
if (!autoCommit) { | ||
commit() | ||
} | ||
} | ||
} | ||
|
||
override fun rollback() { | ||
tObject.connectionHolder.connection.rollback() | ||
} | ||
|
||
override fun close() { | ||
tObject.connectionHolder.connection.close() | ||
} | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...transaction/src/test/kotlin/org.jetbrains.exposed.spring/ExposedTransactionManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.jetbrains.exposed.spring | ||
|
||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType | ||
import org.springframework.transaction.PlatformTransactionManager | ||
import org.springframework.transaction.annotation.EnableTransactionManagement | ||
import org.springframework.transaction.annotation.TransactionManagementConfigurer | ||
import org.springframework.transaction.annotation.Transactional | ||
import javax.sql.DataSource | ||
|
||
|
||
private val ctx = AnnotationConfigApplicationContext(TestConfig::class.java) | ||
|
||
@Transactional | ||
class ExposedTransactionManagerTest { | ||
|
||
@Test | ||
fun testConnection() { | ||
|
||
try { | ||
val pm = ctx.getBean(PlatformTransactionManager::class.java) | ||
if(pm !is ExposedTransactionManager) error("Wrong txManager instance: ${pm.javaClass.name}") | ||
|
||
val t1 = object : Table() { | ||
val c1 = varchar("c1", 5).nullable() | ||
} | ||
|
||
SchemaUtils.create(t1) | ||
t1.insert { | ||
it[c1] = "112" | ||
} | ||
|
||
Assert.assertEquals(t1.selectAll().count(), 1) | ||
|
||
} finally { | ||
(ctx.getBean(DataSource::class.java) as EmbeddedDatabase).shutdown() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
open class TestConfig : TransactionManagementConfigurer { | ||
|
||
@Bean | ||
open fun ds() = EmbeddedDatabaseBuilder().setName("embeddedTest").setType(EmbeddedDatabaseType.H2).build() | ||
|
||
@Bean | ||
override fun annotationDrivenTransactionManager(): PlatformTransactionManager? = ExposedTransactionManager(ds()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.