This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database Connection

 
Nimish Patel
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Have a good day...
we can implement connection using two ways..
1)first, jndi - initial context
2)second , class.forname()-driver manager

in these ways , which is good way..and what is the diffrence between this ?
when implement jndi or driver manager... ?
please clarify this.

Thanks in anticipation.

rgds,
Nimish
[ September 02, 2005: Message edited by: Nimish Patel ]
 
Masoud Kalali
Author
Ranch Hand
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Using the manager to get the connection is hard code way
and using jndi gives you the ability to change the connection properties in deployment .
using manager make you coupled within the connection you made , and in using jndi you are not.
another thing :
I used jndi and datasources while i need the conection to be managed by container and i need connection pooling.
I think its another benefits of using container managed (jndi based) connection management than plain jdbc one.
hope it helps
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Obtaining the Connection from JNDI:
1)That Connection is a Dynamic connection.
2)In JNDI we defined some connection previously (Connectionpool) from here we are obtaining the predefined connection.
3)High performance.
Obtaining the Connection from DriverManger:
1) Is a static connection.
2) We can create our own connection object here.
3)Poor performance for this connection.
Hope that some how cleare.
Regards,
G Sirish Reddy.,
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get accustomed to obtaining a DataSource from a JNDI tree and using it to obtain your Connection. This is the way to write J2EE-compliant apps.
 
Dong Hoang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Datasource to get connection to DB2. When would I need to use ID and password with getConnection? Can I getConnection without ID and password?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No password is needed to get a Connection as the password is supplied on creation of the connection pool on server startup. Each instance in the pool will maintain a connection to the DB. When you look up a data source via JNDI and access a database connection, you get a stub for the Connection instance in the server process.
 
Dong Hoang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this mean I define a default ID and password for DB2 in the server?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, each application server its own way of configuring the username and password.
 
Dong Hoang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I want DB2 to use the ID and password of the user that login from a login page? Can the default ID and password in the server be overwritten in my application?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dong Hoang:
What if I want DB2 to use the ID and password of the user that login from a login page? Can the default ID and password in the server be overwritten in my application?

You can't use a custom user id and password when using the DataSource from the app server. Typically in a web app, you just authenticate the user and use an "application id" for the database access.
 
Don Hoang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure what you meant by "application id" for database access. How do I get this application id?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don,
An "application id" is just a regular database id. You create a user for the database, say "myAppId." Then you use that id with the datasource.

The idea is that if a user is in your application to execute code against the datasource, he/she has already been authenticated.
 
sri subha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know how making use of jndi and datasource gives u flexibility of changing the properties and datasource?

please explain.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sri,
Welcome to JavaRanch! Note that it is ok to ask a new question in a new thread. No need to bump old threads.

Using JNDI to get the datasource allows you to keep all the datasource's information on the server. In this way, it is completely independent of the code. You just change the properties through the server's interface and the code doesn't even have to know that it changed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic