码迷,mamicode.com
首页 > 数据库 > 详细

scalikejdbc 学习笔记(3)

时间:2017-06-24 23:44:58      阅读:666      评论:0      收藏:0      [点我收藏+]

标签:with   use   log   als   throw   name   from   users   session   

重用connection:

package com.citi.scalikejdbc

import scalikejdbc._
import scalikejdbc.config._

object ConnReuse {
  def main(args: Array[String]): Unit = {

    DBsWithEnv("dev").setupAll()

    case class User(id: Int, name: String, age: Int)

    val allColumns = (rs: WrappedResultSet) => User(
      id = rs.int("id"),
      name = rs.string("name"),
      age = rs.int("age"))

    val id = 1
    using(ConnectionPool.borrow()) { conn: java.sql.Connection =>
      val db: DB = DB(conn)

      // set as auto-close disabled
      db.autoClose(false)

      db.localTx { implicit session =>
        sql"update user set name = ‘tom‘ where id = ${id}".update.apply()
      } // localTx won‘t close the current Connection

      db.localTx { implicit session =>
        sql"insert into user values (3, ‘marry‘, 12)".update.apply()
      } // localTx won‘t close the current Connection

      // this block also works fine!
      val users: List[User] = db.localTx { implicit session =>
        SQL("select * from user limit 10").map(allColumns).list.apply()
      }

      for (user <- users) {
        println(user.id + "," + user.name + "," + user.age)
      }

    }

    DBsWithEnv("dev").closeAll()

  }
}

运行结果:  


[SQL Execution]
update user set name = ‘tom‘ where id = 1; (0 ms)

[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$2.apply(ConnReuse.scala:26)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$2.apply(ConnReuse.scala:25)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:25)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...

22:30:30.747 [main] DEBUG scalikejdbc.StatementExecutor$$anon$1 - SQL execution completed

[SQL Execution]
insert into user values (3, ‘marry‘, 12); (1 ms)

[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$3.apply(ConnReuse.scala:30)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$3.apply(ConnReuse.scala:29)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:29)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...

22:30:30.761 [main] DEBUG scalikejdbc.StatementExecutor$$anon$1 - SQL execution completed

[SQL Execution]
select * from user limit 10; (2 ms)

[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$4.apply(ConnReuse.scala:35)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$4.apply(ConnReuse.scala:34)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:34)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...

1,tom,20
2,bill,30
3,marry,12
3,marry,12

scalikejdbc 学习笔记(3)

标签:with   use   log   als   throw   name   from   users   session   

原文地址:http://www.cnblogs.com/AK47Sonic/p/7074693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!