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

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC and Session" Watch "JDBC and Session" New topic

JDBC and Session

Hardik Patel p

Joined: Jan 23, 2014
Posts: 26


why we are not creating connection instance by using new keyword ?

Connection connection =
DriverManager.getConnection(oracleURL, username, password);

same way in the following

HttpSession session = request.getSession();

Thank you
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15665

This is more of a basic Java question than a J2EE question. And, for that matter, we don't encourage use of this kind of code in JSPs, anyway, so this probably wasn't the best forum to ask in.

Java has 2 basic ways to construct/obtain resources.

One is the "new" operator. This operator requests that the JVM should instantiate an object by constructing an instance of a class object and invoking any constructor code it may contain.

The other is the Factory design pattern. Using this technique, you don't use "new" because the actual instantiation of the object is done behind the fa├žade of the factory class or object.

Why use a factory? A factory allows the construction process to reference external context as part of the instantiation. That keeps the constructor methods from having to possibly have 157 parameters and isolates them from the need to radically refactor them in the event of wide-ranging changes to the application.

A factory also allows the use of resource pools. You're using the factory method DriverManager.getConnection, which parses the URL to determine what actual driver you need a connection for (Oracle, in this case). You can thus select a different database (say, PostgreSQL) merely by changing the JDBC URL without changing any actual code.

However, in a webapp, getConnection is bad practice. The recommended method is to use a Connection Pool, which is defined to the server, located via JNDI, and then tapped to obtain Connections as needed, Connection pools are preferable in multi-user applications because the process of actually constructing a Connection instance is slow and resource-intensive, whereas keeping a pool of them already made is much, much faster.

The HttpSession factory is a little different. Unlike DriverManager, where a single class is the factory for all Connections, a HttpServletRequest instance actually stores the assiciated HttpSession object within its internal structure. Only in the event that you've requested a session and none exists (AND the "create session" option has been specified) would the HttpServletRequest manufacture and cache a new HttpSession object.

Customer surveys are for companies who didn't pay proper attention to begin with.
Hardik Patel p

Joined: Jan 23, 2014
Posts: 26

Thank you Mr.Tim Holloway
I agree. Here's the link:
subject: JDBC and Session
Similar Threads
doubt in connection pooling
Firing Native Oracle Queries through hibernate
ejb Stateless SessionBean send JMS msg's
Database Connection and Session
Clean-up objects in session.