Wednesday September 12, 2007
TOTD #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application
Using the instructions followed in JRuby Hack Day and taking some help from Nick, I figured out how to use the JDBC connection pools configured in GlassFish using the JNDI names.
All the commands given below are relevant for GlassFish but the same concept will work where ever you deploy your WARed up JRuby on Rails application.
mysqladmin -u root create jndi_rails_productionDatabase Configurations", "migrate"
and open "001_create_greetings.rb". Change the "self.up"
helper method such that it looks like:def self.up
create_table :greetings do |t|
t.column :data, :string
end
endConfiguration", open "database.yml",
change the database name for development configuration from "jndi_rails_development"
to "jndi_rails_production".Run Rake Target', 'db',
'migrate'. This generates the appropriate database tables and
the following is shown in the output window:(in C:/Users/Arun Gupta/Documents/NetBeansProjects/jndi_rails)
== CreateGreetings: migrating
=================================================
-- create_table(:greetings)
-> 0.2650s
== CreateGreetings: migrated (0.2650s)
========================================C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.0.45-community-nt MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use jndi_rails_production;
Database changed
mysql> insert into greetings values (1, "Hello from MySQL JNDI pool!");
Query OK, 1 row affected (0.03 sec)
mysql> grant all on jndi_rails_production.* to arun@localhost identified
by 'noway';
Query OK, 0 rows affected (0.26 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.16 sec)
mysql> quit;
Byeproduction:
adapter: mysql
database: jndi_rails_production
username: root
password:
host: localhostproduction:
adapter: jdbc
jndi: jdbc/jndi_rails
driver: com.mysql.jdbc.Driver
Notice only JDBC adpater and JNDI name is specified in the
database configuration. This ensures that the database is resolved using
only the JNDI name. Although "database", "username"
and "password" attributes may be specified in addition to "jndi"
and "driver" attributes. In this case, the Rails configuration
falls back to pure-Ruby MySQL adapter.GLASSFISH_HOME\bin, create the JDBC connection pool by
giving the following command:asadmin create-jdbc-connection-pool --datasourceclassname
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype
javax.sql.DataSource --property User=arun:Password=noway:URL=jdbc\:mysql\://localhost/jndi_rails_production
jdbc/jndi_rails_poolCommand create-jdbc-connection-pool executed successfully.asadmin create-jdbc-resource --connectionpoolid jdbc/jndi_rails_pool
jdbc/jndi_railsCommand create-jdbc-resource executed successfully.GLASSFISH_HOME/lib' directory.jndi_rails.war) in "domains/domain/autodeploy"
directory.The application is accessible at "http://localhost:8080/jndi_rails/say/hello".
An alternative approach to use the connection pools is discussed here. Lou also nicely describes the benefits of connection pooling.
Connecting to Oracle From Rails explains how to connect to Oracle (instead of JavaDB) with JRuby.
Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.
Technorati: totd rubyonrails jruby ruby netbeans glassfish connectionpooling jndi jdbc jrubyonglassfish mysql
Posted by Arun Gupta in web2.0 | Comments[9]
|
|
|
|
Today's Page Hits: 7078
Posted by Arun Gupta's Blog on September 18, 2007 at 02:30 PM PDT #
This is all great but wouldn't the main benefit of using the connection pool be that many ruby apps would all share it? Assuming they are each using their own schema, how do you get them to change the context they are executing sql in when they check out a connection from the pool?
Posted by Matt Field on October 01, 2007 at 05:13 AM PDT #
Posted by Arun Gupta's Blog on January 24, 2008 at 04:46 AM PST #
Posted by Arun Gupta's Blog on February 08, 2008 at 04:33 AM PST #
Hi.
It perfect, but what about when you have, let's say 5 databases in the same server and you web application need data from all of them. How do you do that?
Thanks
Greetings
Posted by David on March 02, 2008 at 12:39 PM PST #
David,
How do you do multiple databases in Rails ? I expect the same mechanism will be extended for JNDI as well.
-Arun
Posted by Arun Gupta on March 09, 2008 at 08:49 PM PDT #
Hi,
Can you provide me some link where i can connect to mysql database in glass fish server console for my ejb project on netbeans.
thanks in advance
abishek kumar.
Posted by Abishek Kumar on March 18, 2008 at 04:27 PM PDT #
Hi Abhishek,
Here is a screencast shows how to do this for NetBeans in general:
http://www.netbeans.org/download/flash/netbeans_61/mysql_demo/mysql_demo.html
You should be able to use this for your EJB projects as well.
Thanks,
-Arun
Posted by Arun Gupta on March 18, 2008 at 06:07 PM PDT #
Here is how I solved (from a user in IceFaces tfreyne)
in web.xml
Code:
<!-- first datasource-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/SOME_NAME</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!-- second datasource-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/SOME_NAME2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and in context.xml
Code:
<Context path="/si3_8" docBase="si3_8" debug="1" reloadable="true">
<!-- First one-->
<Resource name="jdbc/SOME_NAME" auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/DATABASE1" username="si3_8"
password="si3" maxActive="20" maxIdle="10" maxWait="-1"
removeAbandoned="true" removeAbandonedTimeout="60"
logAbandoned="true" />
<!-- Second one-->
<Resource name="jdbc/SOME_NAME2" auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/DATABASE2" username="si3_8"
password="si3" maxActive="20" maxIdle="10" maxWait="-1"
removeAbandoned="true" removeAbandonedTimeout="60"
logAbandoned="true" />
</Context>
Posted by David on March 20, 2008 at 02:06 PM PDT #