Skip to content

Commit

Permalink
Update usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sauntor committed Mar 24, 2017
1 parent 0ec7392 commit 23b5c75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ libraryDependencies += "com.lingcreative" %% "play-dbx" % "1.0.1"

class Module extends AbstractModule {
override def configure(): Unit = {
//Use the out-of-boxed TransactionManagerLookup for DBApi, which create a individual DataSourceTransactionManager
//Use the out-of-boxed TransactionManagerLookup for DBApi, which create a
//individual DataSourceTransactionManager
//for each DataSource looked up from `DBApi.database(name).datasource`
bind(classOf[TransactionManagerLookup]).to(classOf[SimpleDBApiTransactionManagerLookup])

Expand All @@ -25,8 +26,8 @@ class Module extends AbstractModule {
val transactionalKey = new TypeLiteral[Transactional[Connection]](){}
bind(transactionalKey).to(classOf[SimpleDBApiTransactional])

//Make the db named `default1` configured in application.conf as the default datasource, and set JDBC isolation level
//to repeatable read
//Make the db named `default1` configured in application.conf as the default datasource,
//and set JDBC isolation level to repeatable read
//Refer [[dbx.transaction.Transactional]]、[[dbx.transaction.Transactional.TransactionSettings]] for details.
val settings = TransactionSettings(isolation = Isolation.REPEATABLE_READ, resource = "default1")
bind(classOf[TransactionSettings]).toInstance(settings)
Expand All @@ -49,7 +50,9 @@ import dbx.api.Transactional
case class Company(id: Option[Long] = None, name: String)

@Singleton
class CompanyService @Inject() (transactional: Transactional[Connection]/*Inject the SimpleDBApiTransactional for JDBC operations*/) {
class CompanyService @Inject() (
/*Inject the SimpleDBApiTransactional for JDBC operations*/
transactional: Transactional[Connection]) {

/**
* Parse a Company from a ResultSet
Expand All @@ -62,7 +65,8 @@ class CompanyService @Inject() (transactional: Transactional[Connection]/*Inject
}

/**
* Get connection from `default1` db configured in application.conf, and rollback if any `Exception` thrown
* Get connection from `default1` db configured in application.conf,
* and rollback if any `Exception` thrown
*/
def options: Seq[(String,String)] = transactional() { implicit connection =>
SQL("select * from company order by name").as(simple *).
Expand All @@ -72,13 +76,19 @@ class CompanyService @Inject() (transactional: Transactional[Connection]/*Inject
}

/**
* Allow update and delete sql to execute, pick connection from `default`, and rollback if any `CompanyExistException`,
* it equals to `@Transactional(readOnly=true, transactionManager="default", rollbackFor={CompanyExistException.class})` in SpringFramework.
* Allow update and delete sql to execute, obtain connections from `default`,
* and rollback if any `CompanyExistException` thrown, it equals to
* `@Transactional(readOnly=true, transactionManager="default", rollbackFor={CompanyExistException.class})`
* in SpringFramework.
*/
def save(company: Company): Unit = transactional(readOnly = false, resource = "default", rollbackFor = Array(classOf[CompanyExistException])) { implicit connection =>
def save(company: Company): Unit = transactional(readOnly = false, resource = "default",
rollbackFor = Array(classOf[CompanyExistException])) { implicit connection =>
// execute sql update statements here ...
}

}

```


**Enjoy it!**
11 changes: 7 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
play-dbx中文说明
play-dbx [English Version](README.md)
================
play-dbx是一个事务管理框架/类库,源代码移植于SpringFramework,并去除无关类和依赖。核心接口为`Transactional``TransactionManagerLookup`(也是需要用户扩展定制的Trait),并提供了DBApi的简单实现。

Expand Down Expand Up @@ -44,7 +44,8 @@ import dbx.api.Transactional
case class Company(id: Option[Long] = None, name: String)

@Singleton
class CompanyService @Inject() (transactional: Transactional[Connection]/*注入Module.scala中配置的Transactional*/) {
class CompanyService @Inject() (
transactional: Transactional[Connection]/*注入Module.scala中配置的Transactional*/) {

/**
* Parse a Company from a ResultSet
Expand All @@ -57,7 +58,8 @@ class CompanyService @Inject() (transactional: Transactional[Connection]/*注入
}

/**
* 按照Module.scala中配置的属性执行事务操作, ` transactional ` 对象的使用方法与Spring的 ` @Transactional ` 注解一致
* 按照Module.scala中配置的属性执行事务操作, ` transactional ` 对象的使用方法与Spring的
* `@Transactional` 注解一致
*/
def options: Seq[(String,String)] = transactional() { implicit connection =>
SQL("select * from company order by name").as(simple *).
Expand All @@ -70,7 +72,8 @@ class CompanyService @Inject() (transactional: Transactional[Connection]/*注入
* 让 ` transactional ` 使用default数据源里的JDBC连接(及对应的事物管理器),并关闭只读属性(即可执行更新操作)。
* 与在Spring里给方法添加注解 ` @Transactional(readOnly=true, transactionManager="default") ` 一致
*/
def save(company: Company): Unit = transactional(readOnly = false, resource = "default") { implicit connection =>
def save(company: Company): Unit = transactional(readOnly = false, resource = "default") {
implicit connection =>
// execute sql update statements here ...
}

Expand Down

0 comments on commit 23b5c75

Please sign in to comment.