Multiple datasource connection at runtime

40 views
Skip to first unread message

Meghal Pandya

unread,
Jan 24, 2019, 6:57:18 AM1/24/19
to ScalikeJDBC Users Group
Hi,
My application allows  user to add multiple datasources. User will fill form by adding database type and credentials and can query for databases, tables, schemas, column metadata and preview data. How should driver be loaded and connection pool be set up in the given scenario?

Kazuhiro Sera

unread,
Jan 24, 2019, 5:15:33 PM1/24/19
to Meghal Pandya, ScalikeJDBC Users Group
You can do that as below:

// add a new JDBC driver and initialize a connection pool
def initialize(driverClassName: String, url: String, user: String,
password: String) = {
Class.forName(driverClassName)
var connectionPoolName = url // connection pool name needs to be a
unique value
ConnectionPool.add(connectionPoolName, url, user, password)
someDataStore.register(connectionPoolName) // save the connection
pool name for later
}

def runQuery(connectionPoolName: String, query: SQL): Seq[Map[String, Any]] = {
NamedDB(connectionPoolName).readOnly { implicit session =>
query.map(_.toMap).list.apply()
}
}

On Thu, Jan 24, 2019 at 8:57 PM Meghal Pandya <meghal....@gmail.com> wrote:
>
> Hi,
> My application allows user to add multiple datasources. User will fill form by adding database type and credentials and can query for databases, tables, schemas, column metadata and preview data. How should driver be loaded and connection pool be set up in the given scenario?
>
> --
> You received this message because you are subscribed to the Google Groups "ScalikeJDBC Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Meghal Pandya

unread,
Jan 31, 2019, 2:46:21 AM1/31/19
to ScalikeJDBC Users Group
Thanks for the reply. Conenction pool object internally uses mutable map to access connection pool via name. Is there any way to keep cap on that i.e. maximum number of connection pool at a time.
> To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users-group+unsub...@googlegroups.com.

Kazuhiro Sera

unread,
Jan 31, 2019, 3:32:15 AM1/31/19
to Meghal Pandya, ScalikeJDBC Users Group
>Is there any way to keep cap on that i.e. maximum number of connection pool at a time.

No, there isn't for now. The implementation is the standard mutable
HashMap. Of course, it's possible to have a functionality to check the
size in ConnectionPool.scala. If you really need it, send a pull
request or copy the code and have your own connection pool
implementation.

Kaz
>> > To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "ScalikeJDBC Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages