This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

sun one app server and connection pool

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind, I figured it out. You cannot use blank passwords :-(.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic