aspose file tools*
The moose likes JDBC and the fly likes Why JNDI for database connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Why JNDI for database connection" Watch "Why JNDI for database connection" New topic
Author

Why JNDI for database connection

ramdas pradeep kumar
Ranch Hand

Joined: Aug 27, 2003
Posts: 36
I came across the following code in a servlet
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(config.getInitParameter("DATASOURCE_NAME"));
Is that soething to do with JNDI data connection. is that refers a JDBC driver.
Nagendra Prasad
Ranch Hand

Joined: Jul 11, 2002
Posts: 219
It is the way of obtaining a data source via JNDI.
The data source could have been defined in the application server and given a jndi name. This JNDI name could be stored in some configuration file, (could be just a whole bunch of Static final parameters).. and this is being retrieved and used.


Best Regards,<br />Nagendra Prasad.
vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
well Nagendra
i got a confusion about JNDI processing here..
JNDI will look for datasource(name)depending upon the key provided in configuration file.
How do it gets the location of this database?
thanks
Vikas
Originally posted by Nagendra Prasad:
It is the way of obtaining a data source via JNDI.
The data source could have been defined in the application server and given a jndi name. This JNDI name could be stored in some configuration file, (could be just a whole bunch of Static final parameters).. and this is being retrieved and used.


Thanks
Vikas Sharma
SCJP(1.4)
Nagendra Prasad
Ranch Hand

Joined: Jul 11, 2002
Posts: 219
vikas
i can reply to this in the context in which i have addressed the problem.
The datasource is defined for the app server. THis I achieved using the admin interface for the app server. The server resources generally have a section where u ca specify the JDBC providers. In the same place u can provide a database name, username and password.
While defining, once can provide a JNDI mapping (or name) for this datasource and reference it as such within the application.
vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
well Nagendra i m also asking in same context.
lets say my database for which datasource is to be created is at some other location.
In configuration section, we also hard code the machine location of database.
Is it possible to move databse location to some other machine while the application is running?
Like in case we want to move EJB to other location,the user is still serviced due to inbuilt location transparency property.
Can we utilize location transparency in case of persistence server also(database)without hardcoding machine location in configuration section?
thanks.
Originally posted by Nagendra Prasad:
vikas
i can reply to this in the context in which i have addressed the problem.
The datasource is defined for the app server. THis I achieved using the admin interface for the app server. The server resources generally have a section where u ca specify the JDBC providers. In the same place u can provide a database name, username and password.
While defining, once can provide a JNDI mapping (or name) for this datasource and reference it as such within the application.
Nagendra Prasad
Ranch Hand

Joined: Jul 11, 2002
Posts: 219
Vikas...
to be honest.. i am not sure of answer for this...
i was (and still am) under the impression that any change to config parameters of an appserver needs a restart... that being the case I am not sure if service level can continously be maintained..
even in a clustered environment, i am wondering if such a thing is easily possible..
not sure... but a good trail tothink of .. if u find anything.. could you please post? If I get something out of my research, I shall..
ramdas pradeep kumar
Ranch Hand

Joined: Aug 27, 2003
Posts: 36
My problem was solved.

Regarding the discussion about the db path:
I am telling in the context of Tomcat server.
in the config/server.xml we have to create context for each application. there we give all the resource and resource parameters.
in this point we specify some DSN or file path. like
........
<parameter>
<name>url</name>
<value>jdbc:interbase://localhost/c:/app/dbs_myapp.gdb</value>
</parameter>
........
.......
but don't know about EJBs and all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why JNDI for database connection