标签: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
标签:with use log als throw name from users session
原文地址:http://www.cnblogs.com/AK47Sonic/p/7074693.html