Skip to content

Commit 7118bf1

Browse files
author
David Pratt
committed
Fix problem where Database referenced Spring.
1 parent 91a1d32 commit 7118bf1

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/main/scala/janus/Database.scala

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ import scala.concurrent._
77
import org.springframework.transaction.PlatformTransactionManager
88

99
object Database {
10-
/**
11-
* Create a new Database. Sessions created by this database will manage their own transaction semantics.
12-
*
13-
* Note - if this datasource is synchronized to a Spring PlatformTransactionManager, usage of a Database returned from
14-
* this method will interfere with and likely cause undefined behavior with Spring managed transactions.
15-
*/
16-
def apply(ds: DataSource) = new JdbcDatabase(ds)
17-
18-
/**
19-
* Create a new Database that will create and participate in Spring managed transactions.
20-
*
21-
* Note - the supplied DataSource *MUST* be synchronized by the supplied PlatformTransactionManager,
22-
* or undefined (and likely destructive) behavior will occur.
23-
*/
24-
def apply(ds: DataSource, txManager: PlatformTransactionManager) = new SpringDatabase(ds, txManager)
25-
2610
private val logger = LoggerFactory.getLogger(classOf[Database])
2711
}
2812

@@ -61,10 +45,21 @@ trait Database {
6145

6246
}
6347

64-
private[janus] class JdbcDatabase(ds: DataSource) extends Database {
48+
/**
49+
* A plain Database implementation that does it's own transaction management.
50+
*
51+
*/
52+
class JdbcDatabase(ds: DataSource) extends Database {
6553
def createSession: Session = new SimpleSession(ds)
6654
}
6755

68-
private[janus] class SpringDatabase(ds: DataSource, txManager: PlatformTransactionManager) extends Database {
56+
/**
57+
* A Database that can participate in Spring managed transactions. This Database will delegate all transaction
58+
* semantics to Spring, and is capable of creating and participating in a thread-bound transaction context.
59+
*
60+
* IMPORTANT - The supplied DataSource *MUST* be synchronized by the provided PlatformTransactionManager. If it is not,
61+
* undefined (and likely destructive) behavior will occur.
62+
*/
63+
class SpringDatabase(ds: DataSource, txManager: PlatformTransactionManager) extends Database {
6964
def createSession: Session = new SpringSession(ds, txManager)
7065
}

src/test/scala/janus/DatabaseSpec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class DatabaseSpec extends FlatSpec {
1212

1313
def basicDatabase = {
1414
val ds = emptyDataSource()
15-
Database(ds)
15+
new JdbcDatabase(ds)
1616
}
1717

1818
def springDatabase = {
1919
val ds = emptyDataSource()
20-
Database(ds, new DataSourceTransactionManager(ds))
20+
new SpringDatabase(ds, new DataSourceTransactionManager(ds))
2121
}
2222

2323
"A Database based on a simple DataSource" should behave like basicDatabaseBehavior(basicDatabase)

src/test/scala/janus/JTASpec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class JTASpec extends FlatSpec {
1515
"A database" should "properly participate in an XA tranasaction with a single datasource" in {
1616
val ds = emptyDataSource("first-ds")
1717
val txManager = transactionManager
18-
val db = Database(ds, new JtaTransactionManager(txManager, txManager))
18+
val db = new SpringDatabase(ds, new JtaTransactionManager(txManager, txManager))
1919
try {
2020
db.withSession { session =>
2121
session.executeQuery("select count(*) from test") { rs =>
@@ -52,7 +52,7 @@ class JTASpec extends FlatSpec {
5252
val unmanagedDs = emptyDataSource("first-ds")
5353
val managedDs = emptyDataSource("second-ds")
5454
val txManager = transactionManager
55-
val managedDatabase = Database(unmanagedDs, new JtaTransactionManager(txManager, txManager))
55+
val managedDatabase = new SpringDatabase(unmanagedDs, new JtaTransactionManager(txManager, txManager))
5656
try {
5757

5858
def validateUnmanaged(expectedCount: Int) {

0 commit comments

Comments
 (0)