dcsimg
Proper handling of javax.naming.InitialContext
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Adam_Rabung
Posted On:   Saturday, January 12, 2002 08:31 AM

I've seen conflicting advice on the proper lifetime of a javax.naming.InitialContext object. On on hand, some people advise that an InitialContext is a limited resource, to be created when needed, and released shortly thereafter, like a database connection. Others have advised that intantiation is the only resource-intensive aspect of InitialContext, and they should be "kept around" by an object as an instance variable to avoid redundant instantiation. More simply, should InitialContexts be used like this: public class SimpleServlet extends HTTPServlet { private InitialContext myContext; public void init(ServletConfig _config) { myContext = new InitialContext(); ..... }    More>>

I've seen conflicting advice on the proper lifetime of a javax.naming.InitialContext object. On on hand, some people advise that an InitialContext is a limited resource, to be created when needed, and released shortly thereafter, like a database connection. Others have advised that intantiation is the only resource-intensive aspect of InitialContext, and they should be "kept around" by an object as an instance variable to avoid redundant instantiation. More simply, should InitialContexts be used like this:

			
public class SimpleServlet extends HTTPServlet {
private InitialContext myContext;

public void init(ServletConfig _config) {
myContext = new InitialContext();
.....
}

..some methods that use getContext()..

private InitialContext getContext() {
return myContext;
}


Or, should the context be more short lived:
			
public class SimpleServlet extends HTTPServlet {

public void init(ServletConfig _config) {
.....
}

..some methods that use getContext()..
..they also take care to close InitialContext objects..

public InitialContext getContext() {
return new InitialContext();
}
   <<Less
About | Sitemap | Contact