java - Jython and standalone DB connection pool: How to confirm the pool is a Singleton -
question: can utilize zxjdbc.connectx
method, outside of application server container?
i have own server application in jython, , i'm looking upgrade using database connection pool (since i'm building , destroying individual connections manually, @ point). have found sample code , have gotten work (using tomcat's connection pool), way works bothers me. me, looks pool getting created on , on again. here's working example:
from __future__ import with_statement com.ziclix.python.sql import zxjdbc params = { } params['url'] = 'jdbc:mysql://localhost:3306/my_database' params['driverclassname'] = 'com.mysql.jdbc.driver' params['username'] = 'mario' params['password'] = 'myp@ssw0rd' params['validationquery'] = 'select 1' params['jdbcinterceptors'] = \ 'org.apache.tomcat.jdbc.pool.interceptor.connectionstate;' + \ 'org.apache.tomcat.jdbc.pool.interceptor.statementfinalizer' # line worries me! conn = zxjdbc.connectx('org.apache.tomcat.jdbc.pool.datasource', **params) conn.cursor() cursor: cursor.execute('select * mytable') info = cursor.fetchall() print info conn.close()
take @ "the line worries me." utilize zxjdbc
connection object, if each time obtain have give datasource class name along setup parameters, first thing think connection pool beingness created anew each time. that's obviously not want.
does know sure what's going on, or how might go confirming what's going on—with little experimentation, perhaps? supposed duplicate jndi infrastructure of servlet container or if want utilize in server? failing understand datasource
, how works? don't know how bottom of it. thanks!
edit: jython source code
i took @ jython source code. connectx
method backed com.ziclix.python.sql.connect.connectx
class. relevant snippet looks this:
/** * build javax.sql.datasource or javax.sql.connectionpooleddatasource */ @override public pyobject __call__(pyobject[] args, string[] keywords) { connection c = null; pyconnection pc = null; object datasource = null; pyargparser parser = new pyargparser(args, keywords); seek { string klass = (string) parser.arg(0).__tojava__(string.class); datasource = class.forname(klass).newinstance(); } grab (exception e) { throw zxjdbc.makeexception(zxjdbc.databaseerror, "unable instantiate datasource"); } /* * code continues on, setting connection pool's parameters, * handling errors, etc., , obtaining connection (variable: c). */ seek { if (c == null || c.isclosed()) { throw zxjdbc.makeexception(zxjdbc.databaseerror, "unable found connection"); } pc = new pyconnection(c); } grab (sqlexception e) { throw zxjdbc.makeexception(zxjdbc.databaseerror, e); } homecoming pc; }
building jython source isn't easy (there appear not well-documented dependencies), or set in debugging statements compare datasource
objects. when seek duplicate creation part on own...
datasource = class.forname(klass).newinstance();
...it looks me unique datasource
instances (and therefore, presumably, unique pool instances) beingness created each call.
does have experience jython , know sure? thanks.
based on research, have come next solution.
instead of using more direct approach shown in jython illustration code, instantiate datasource
using java methods. (i took illustration code on apache tomcat site.) then, instead of calling zxjdbc method obtain connection object, create utilize of method relies on: namely, com.ziclix.python.sql.pyconnection
class.
the result traditional java connection object drawn pool, wrapped in pyconnection object, allowing convenience of jython connection , cursor object.
from __future__ import with_statement com.ziclix.python.sql import pyconnection import org.apache.tomcat.jdbc.pool pool # https://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html p = pool.poolproperties() p.seturl('jdbc:mysql://localhost:3306/my_database') p.setdriverclassname('com.mysql.jdbc.driver') p.setusername('mario') p.setpassword('myp@ssw0rd') p.setvalidationquery("select 1") p.setjdbcinterceptors('org.apache.tomcat.jdbc.pool.interceptor.connectionstate;' + 'org.apache.tomcat.jdbc.pool.interceptor.statementfinalizer') datasource = pool.datasource() datasource.setpoolproperties(p) # http://www.jython.org/javadoc/com/ziclix/python/sql/pyconnection.html conn = pyconnection(datasource.getconnection()) conn.cursor() cursor: cursor.execute('select * mytable') info = cursor.fetchall() print info conn.close()
i have more confidence in approach. there reason why shouldn't?
java tomcat database-connection jython connection-pooling
No comments:
Post a Comment