Follow-up:

The issue is that sequel/adapters/jdbc/postgresql.rb presumes that an 
escape_string method is present on the connection, which is true when working 
with a org.postgresql.jdbc4.Jdbc4Connection. However, if you set up a JNDI 
datasource in Tomcat, you end up with a DBCP that hands out 
org.apache.commons.dbcp.PoolingDataSource.PoolGuardConnectionWrapper instances, 
which have no such escape_string method. Hence the failure.

If you put a Postgres-specific datasource in JNDI it works fine. See e.g. 
http://jdbc.postgresql.org/documentation/head/jndi.html. This works for me; I 
have no particular need to use Tomcat's context.xml as my configuration 
mechanism. Another alternative would be to monkey-patch 
Sequel::JDBC::Postgres::Dataset's literal_string method to have an appropriate 
Ruby-based escaping mechanism rather than calling the one on the Postgres 
driver. Not too much munging involved, see 
http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/postgresql/core/Utils.java
 appendEscapedLiteral method.

BTW, sequel/adapters/postgres.rb is never supposed to run in the JDBC case; 
that was a red-herring.

-Lorrin

On Jun 22, 2011, at 5:10 PM, Lorrin Nelson wrote:

> I had a working connection to a Postgres db. Now I'm trying to use JNDI and 
> running into problems. Sequel connects but then cannot do useful work. 
> Judging by the trace, it seems like the PGconn munging magic in 
> sequel/adapters/postgres.rb hasn't been applied to the Connection. Perhaps 
> this a result of switching to having Tomcat provide the connection pool 
> rather than Sequel establishing its own connections and I need some way to 
> get Sequel to layer its initialization on top of the connections received 
> from Tomcat? Hrm.
> 
> FWIW, the database URL I'm using is jdbc:jndi:java:comp/env/jdbc/my_db and I 
> defined the JDNI datasource in my Tomcat context.xml like this: <Resource 
> name="jdbc/my_db" auth="Container" type="javax.sql.DataSource" 
> maxActive="100" maxIdle="30" maxWait="10000" username="db_user" 
> password="password" driverClassName="org.postgresql.Driver" 
> url="jdbc:postgresql://localhost/mydatabase"/>
> 
> java.util.concurrent.ExecutionException: 
> org.jruby.rack.RackInitializationException: undefined method `escape_string' 
> for #<#:0x4de35d1a> 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/connection_pool/threaded.rb:84:in
>  `hold' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/database/connecting.rb:226:in
>  `synchronize' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/jdbc/postgresql.rb:91:in
>  `literal_string' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:82:in
>  `literal' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:294:in
>  `complex_expression_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/shared/postgres.rb:685:in
>  `complex_expression_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/sql.rb:72:in 
> `to_s' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:683:in
>  `literal_expression' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:90:in
>  `literal' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:296:in
>  `complex_expression_sql' 
> from org/jruby/RubyArray.java:2306:in `collect' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:296:in
>  `complex_expression_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/shared/postgres.rb:685:in
>  `complex_expression_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/sql.rb:72:in 
> `to_s' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:683:in
>  `literal_expression' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:90:in
>  `literal' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:906:in
>  `select_where_sql' 
> from org/jruby/RubyKernel.java:2042:in `send' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:540:in
>  `clause_sql' 
> from org/jruby/RubyArray.java:1572:in `each' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:540:in
>  `clause_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/sql.rb:131:in
>  `select_sql' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/actions.rb:123:in
>  `each' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/actions.rb:449:in
>  `single_record' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/dataset/actions.rb:184:in
>  `first' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/shared/postgres.rb:380:in
>  `table_exists?' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/shared/postgres.rb:394:in
>  `tables' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/adapters/shared/postgres.rb:380:in
>  `table_exists?' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/extensions/migration.rb:493:in
>  `schema_dataset' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/extensions/migration.rb:380:in
>  `initialize' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/sequel-3.21.0/lib/sequel/extensions/migration.rb:421:in
>  `initialize' 
> from 
> /Applications/apache-tomcat-6.0.29/webapps/mywebapp/WEB-INF/config/initializers/automigrate.rb:14:in
>  `run' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/merb-core-1.1.3/lib/merb-core/bootloader.rb:97:in
>  `run' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/merb-core-1.1.3/lib/merb-core/server.rb:172:in
>  `bootup' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/merb-core-1.1.3/lib/merb-core/server.rb:42:in
>  `start' 
> from 
> /Users/lhn/.rvm/gems/jruby-1.6.0/gems/merb-core-1.1.3/lib/merb-core.rb:165:in 
> `start' 
> from 
> file:/Applications/apache-tomcat-6.0.29/webapps/mywebapp/WEB-INF/lib/jruby-rack-1.0.8.ccllc.2.jar!/jruby/rack/merb.rb:48:in
>  `start_merb' 
> from 
> file:/Applications/apache-tomcat-6.0.29/webapps/mywebapp/WEB-INF/lib/jruby-rack-1.0.8.ccllc.2.jar!/jruby/rack/merb.rb:33:in
>  `load_merb' 
> from 
> file:/Applications/apache-tomcat-6.0.29/webapps/mywebapp/WEB-INF/lib/jruby-rack-1.0.8.ccllc.2.jar!/jruby/rack/merb.rb:60:in
>  `new' 
> from null:1:in `(root)' 
> from org/jruby/RubyKernel.java:2007:in `instance_eval' 
> from 
> file:/Applications/apache-tomcat-6.0.29/webapps/mywebapp/WEB-INF/lib/jruby-rack-1.0.8.ccllc.2.jar!/vendor/rack-1.2.2/rack/builder.rb:46:in
>  `initialize' 
> from null:1:in `(root)'
> 
> 
> Thanks
> -Lorrin

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to