Created
December 19, 2015 02:43
-
-
Save johntbush/9ad651f2f947c553eb7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class TaggedDB(name: Any)(implicit context: ConnectionPoolContext = NoConnectionPoolContext) extends DBConnection with LazyLogging { | |
private[this] def connectionPool(): ConnectionPool = Option(context match { | |
case NoConnectionPoolContext => ConnectionPool(name) | |
case _: MultipleConnectionPoolContext => context.get(name) | |
case _ => throw new IllegalStateException(ErrorMessage.UNKNOWN_CONNECTION_POOL_CONTEXT) | |
}) getOrElse { | |
throw new IllegalStateException(ErrorMessage.CONNECTION_POOL_IS_NOT_YET_INITIALIZED) | |
} | |
override def connectionAttributes: DBConnectionAttributes = { | |
connectionPool().connectionAttributes | |
} | |
private lazy val db: DB = { | |
val cp = connectionPool() | |
DB(cp.borrow(), connectionAttributes) | |
} | |
def toDB(): DB = db | |
def conn: Connection = db.conn | |
def tags(name:Any, conn:Connection):Seq[String] = { | |
if (!name.isInstanceOf[String]) return Seq.empty[String] | |
val nameStr = name.asInstanceOf[String] | |
val pieces = nameStr.split("/") | |
val (server, dbName) = if (pieces.size > 1) (pieces(0), pieces(1)) else (nameStr, "???") | |
Seq( | |
server, | |
dbName, | |
conn.getMetaData.getURL, | |
conn.getMetaData.getUserName, | |
conn.getMetaData.getDriverName) | |
} | |
override def readOnlySession(): DBSession = { | |
val session = DBSession(conn, isReadOnly = true, connectionAttributes = connectionAttributes) | |
session.tags(tags(name, session.connection):_*) | |
} | |
override def autoCommitSession(): DBSession = { | |
val session = DBSession(conn, isReadOnly = true, connectionAttributes = connectionAttributes) | |
session.tags(tags(name, session.connection):_*) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment