简体   繁体   中英

Scala Slick c3p0 connection pool using mysql

I'm a total newbie to scala and slick in particular. I'm trying to use c3p0 connection pool with MySQL backend and slick.

**** 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)
  }
}

Error

could not find implicit value for parameter session: scala.slick.jdbc.JdbcBackend#SessionDef
not enough arguments for method +=: (implicit session: scala.slick.jdbc.JdbcBackend#SessionDef)Int. Unspecified value parameter session.

scalaVersion : 2.11.1

mysqlVersion: 5.1.26

c3p0Version: 0.9.2.1

Link to a good MySql , slick, c3p0 example is sought.

Thanks...

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)
}
 }

try this. You are missing implicit session parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM