0
votes

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...

1

1 Answers

1
votes
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.