File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Java Products and Servers and the fly likes sun one app server and connection pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "sun one app server and connection pool" Watch "sun one app server and connection pool" New topic
Author

sun one app server and connection pool

ben ku
Greenhorn

Joined: Mar 30, 2004
Posts: 2
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
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
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
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
Never mind, I figured it out. You cannot use blank passwords :-(.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sun one app server and connection pool