Advertisement
NLinker

Postgres CopyManager with c3p0 connection pool

Oct 22nd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.46 KB | None | 0 0
  1.   def writeBuffersToDb(files: Seq[File]) = {
  2.     println(s"Writing buffers $files")
  3.     def diff(ts: Long) = (System.currentTimeMillis() - ts) / 1000
  4.     // parallel works suspiciously slow under windows,
  5.     // however it is expected to be faster
  6.     // http://dba.stackexchange.com/questions/20723/postgresql-two-concurrent-copy-from
  7.     DB localTx { implicit session: DBSession =>
  8.       // dropIndexes()
  9.       files.foreach { case file =>
  10.         val ts = System.currentTimeMillis()
  11.         println(s"Processing $file")
  12.         Using.using(new FileInputStream(file)) { stream ⇒
  13.           val field = session.connection.getClass.getDeclaredField("this$0")
  14.           field.setAccessible(true)
  15.           val ds = field.get(session.connection).asInstanceOf[PoolingDataSource]
  16.           ds.setAccessToUnderlyingConnectionAllowed(true)
  17.           val conn = session.connection.asInstanceOf[DelegatingConnection].getInnermostDelegate
  18.           val copyManager = new CopyManager(conn.asInstanceOf[BaseConnection])
  19.           // val reader = new FileReader(file)
  20.           //"COPY timeseriesentry(ts, val, componentgroup_id) FROM STDIN",
  21.           copyManager.copyIn("COPY observation(ts, vl, parameter_id) FROM STDIN;", stream)
  22.         }
  23.         println(s"Done $file in ${diff(ts)} seconds")
  24.       }
  25.       println(s"Recreating index...")
  26.       val ts = System.currentTimeMillis()
  27.       // createIndexes()
  28.       println(s"Done index in ${diff(ts)} seconds")
  29.     }
  30.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement