This project demonstrates how to use datasource-proxy from JNDI using Tomcat.
- If
ProxyDataSource
is configued as a JNDI resource and passed to the application asjavax.sql.DataSource
, then only container needs to knowdatasource-proxy.jar
. Your application doesn't need a dependency todatsource-proxy.jar
.
Setup uses Tomcat and HSQLDB.
-
Download
hsqldb.jar
anddatasource-proxy.jar
(1.3+) place them to${CATALINA_HOME}/lib
-
Configure Tomcat
-
${CATALINA_HOME}/conf/server.xml
<!-- Define actual datasource `jdbc/global/myDS` using embedded hsqldb. --> <Resource name="jdbc/global/myDS" auth="Container" type="javax.sql.DataSource" description="ds" driverClassName="org.hsqldb.jdbc.JDBCDriver" url="jdbc:hsqldb:mem:aname" /> <!-- * Define `ProxyDataSource` with `DataSourceQueryCountListener` and `SystemOutQueryLoggingListener` --> <Resource name="jdbc/global/myProxy" auth="Container" type="net.ttddyy.dsproxy.support.ProxyDataSource" factory="net.ttddyy.dsproxy.support.jndi.ProxyDataSourceObjectFactory" description="ds" listeners="count,sysout" proxyName="DS-PROXY" format="json" dataSource="java:jdbc/global/myDS" />
-
${CATALINA_HOME}/conf/context.xml
<!-- Expose defined `ProxyDataSource` to application as a `DataSource` --> <ResourceLink name="jdbc/myDS" global="jdbc/global/myProxy" type="javax.sql.DataSource" />
- build this application and deploy to tomcat
> mvn package
// or mvn war:war, etc
- copy war to
${CATALINA_HOME}/webapps
, or symlink, etc
- start tomcat and access the url
Sample Output
Application startup log:
{"name":"DS-PROXY", "time":1, "success":true, "type":"Statement", "batch":false, "querySize":1, "batchSize":0, "query":["CREATE TABLE users(id INT, name VARCHAR(255))"], "params":[]}
{"name":"DS-PROXY", "time":0, "success":true, "type":"Prepared", "batch":true, "querySize":1, "batchSize":3, "query":["INSERT INTO users (id, name) VALUES (?, ?)"], "params":[{"1":"1","2":"foo"},{"1":"2","2":"bar"},{"1":"3","2":"baz"}]}
Accessing application:
> curl http://localhost:8080/dsp-lookup/
{"count":3}
Log:
{"name":"DS-PROXY", "time":6, "success":true, "type":"Statement", "batch":false, "querySize":1, "batchSize":0, "query":["SELECT COUNT(*) FROM users"], "params":[]}