使用mysql的Scala Slick c3p0连接池

时间:2014-10-09 17:55:47

标签: mysql scala slick c3p0

我特别喜欢scala和slick。我正在尝试使用带有MySQL后端和光滑的c3p0连接池。

**** Persons.scala

scala.slick.driver.MySQLDriver.simple._
import scala.slick.lifted.ProvenShape    
class Persons(tag: Tag) extends Table[(Option[Long], String, Long)](tag, "PERSONS") {
  def id: Column[Long] = column[Long]("ID", O.PrimaryKey, O.AutoInc)    
  def name: Column[String] = column[String]("NAME")
  def updatedDt: Column[Long] = column[Long]("UPDATED_DT")

  def * : ProvenShape[(Long, String, Long)] = 
(id, name, updatedDt)
}

**** PersonDao.scala

import scala.slick.driver.MySQLDriver.simple._
object PersonDao extends App with DatabaseAccess {
   val persons: TableQuery[Persons] = TableQuery[Persons]
def insert(id:Option[Long] = None, name: String, updatedDt: Long) = databasePool withSession { 
  persons += (id, name, updatedDt)
 }
}

**** DatabaseAccess.scala

import scala.slick.driver.MySQLDriver.simple.Database
import com.mchange.v2.c3p0.ComboPooledDataSource
trait DatabaseAccess {
  val Url = "jdbc:mysql://192.168.10.12:3306/person"
  val Driver = "com.mysql.jdbc.Driver"
  val database = Database.forURL(Url, driver = Driver)
  val databasePool = {
  val ds = new ComboPooledDataSource
  ds.setDriverClass(Driver)
  ds.setJdbcUrl(Url)
  ds.setMinPoolSize(20)
  ds.setAcquireIncrement(5)
  ds.setMaxPoolSize(100)
  Database.forDataSource(ds)
  }
}

错误

无法找到参数会话的隐含值:scala.slick.jdbc.JdbcBackend #SessionDef
没有足够的方法参数+ = :(隐式会话:scala.slick.jdbc.JdbcBackend #SessionDef)Int。未指定的值参数会话。

scalaVersion:2.11.1

mysqlVersion:5.1.26

c3p0版本:0.9.2.1

寻找一个好的MySql链接,光滑,c3p0示例。

...谢谢

1 个答案:

答案 0 :(得分:1)

import scala.slick.driver.MySQLDriver.simple._
object PersonDao extends App with DatabaseAccess {
val persons: TableQuery[Persons] = TableQuery[Persons]
def insert(id:Option[Long] = None, name: String, updatedDt: Long) = databasePool 
withSession {   implicit session =>
   persons += (id, name, updatedDt)
}
 }

试试这个。 您缺少隐式会话参数。