| Author |
Static method for Obtaining Connection Object in web application
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi
I have written a Template for getting Oracle Connection for my Application ?? Will it be good if my make this method static
For example
|
Save India From Corruption - Anna Hazare.
|
 |
Rok Ć telcer
Ranch Hand
Joined: Nov 03, 2009
Posts: 101
|
|
Hi,
Ravi Kiran V wrote:I have written a Template for getting Oracle Connection for my Application
What do you mean a template ... like a template pattern?
If so, this is not a template pattern (see: Template) ... would have to say more of a singleton.
Anyhow, regarding the code.
Perhaps your getConnection() method should return only a connection, without any init or lookup stuff in it?
Meaning you could create a:
- lazy loading singleton (created on demand ... first use)
- or put the init in a static initializer (created when class is loaded ... if not too early)
- or make a inner wrapper for the initialization and again use a singleton
Beside this, you'll have to catch following two execptions: SQLException & NamingException.
edit: or since this is a static method, you should perhaps re-thrown some custom exception with appropriate msg.
Hope this helps.
Regards,
Rok
|
SCJP, SCWCD
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
First of all thanks for all the knowledge Transfer .
I purposefully did not add any Exception handling to make it look simpler .
The basic idea of posting this question is that , Mine is a web based Application , where more than 100 users can access the Application simultaneously , so i created a Connection Pool with a Capacity of 100 .
Now my requirement is that i want to allow a single user Obtain only One Connection(Even though he opens a new window of my application) so please tell me how to achieve this . (I think that i cannot make a blunder with going for static methods for Obtaining Connections as this is a web application).
and also i think that there is a difference between Template and a Template Method pattern(Correct me if i am wrong).
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
In theory, you could use the caller's IP address and code so that you restrict the number of connections. But this is a bad idea.
* Users could come from a common router, in which case all clients will have the router's IP
* You cannot restrict a user from using more than one connection. How do you handle cases where the connection went sour and how do you track these changes ?
* Writing your own connection pool can be a big design challenge. For example, your static method cannot restrict the number of Connections to 100. A singleton per server instance will be able to. However even the singleton has to keep track of Connections and when they need to be released. If a Connection is closed the pool has to be refreshed with a new Connection. Handling all these transitions requires a lot of testing, which the app server providers have already implemented and done for you.
Long story short, use the connection pool provided by the app server
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Khalid Elousami
Greenhorn
Joined: Dec 03, 2008
Posts: 4
|
|
Hi Ravi,
"100 simultaneous user" means "100 simultaneous user requests", so there's absolutly no need to restrict connections to actual users.
You rely on a datasource for connections, which should rely on a connection pool which is configured with max conn = 100.
What have to be done is to ensure connections are closed asap so to be returned to the pool.
Good luck!
|
 |
 |
|
|
subject: Static method for Obtaining Connection Object in web application
|
|
|