Hi, I'm using SunOne AS7 and a mysql DB. I have a Java class that does all the work with the DB :
and so on. I would like to use a connection pool but I don't know how to do that. On the admin pages of the app server, I've added a connection pool in Server1/JDBC/Connection Pools and I would like to know what I have to change in my code to use this connection pool. thanks
C Kutler
Ranch Hand
Joined: Apr 15, 2004
Posts: 62
posted
0
I did this once and it took a long time to figure out. It didn't help that their book gave some wrong code. They wanted something like Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/quoting"); I just messed around until I got something that worked. Here is snippet. public void init() { /* Init shared resources */ try { Context initCtx = new InitialContext(); DataSource ds = (DataSource) initCtx.lookup("env/jdbc/quoting"); dbCon = ds.getConnection(); } catch (javax.naming.NamingException e) { System.out.println( "A problem occurred while retrieving a DataSource object"); System.out.println(e.toString()); } catch (java.sql.SQLException e) { System.out.println( "A problem occurred while connecting to the database."); System.out.println(e.toString()); }
We learn by doing, there is no other way.
Hastono Bayu
Greenhorn
Joined: Oct 13, 2003
Posts: 8
posted
0
maybe this can help.. to create a connection pool, use the application server admin console default on http://localhost:4848 on the JDBC - Connection Pool - create new here's an example Name : MySQLPool ResourceType : java.sql.DataSource Vendor : Mysql Data Source class name : com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource and add the following property user : db_user password : db_password port : 3306 (mysql default) that's it. remember to put the MySQL connector/J jar file to the server classpath and then create a JDBC Resources afterward for ex, JNDI name : jdbc/MySQL PoolName MySQLPool --> use the pool u've created b4 status enabled To create a connection to this pool simple use this code on the init section: InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/MySQL"); Connection conn = ds.getConnection() ; //"creating statement.." Statement stmt = conn.createStatement(); and do rest of the query as usual hope it will help
Regards,
Bayu
Fernando Margueirat
Ranch Hand
Joined: Jun 07, 2004
Posts: 33
posted
0
Hi Hastono
I did what you mentioned in this post but I'm getting a NullPointerException when the
Connection conn = ds.getConnection();
statement is excecuted.
Any ideas what could I be missing or how can I debug the appliction to get more information?
I also noticed that when I do the lookup the Object I get back is of Class class com.sun.gjc.spi.DataSource. Is this correct?
Thanks
Fernando Margueirat
Ranch Hand
Joined: Jun 07, 2004
Posts: 33
posted
0
Never mind, I figured it out. You cannot use blank passwords :-(.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.