aspose file tools*
The moose likes JSP and the fly likes Driver Loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Driver Loading" Watch "Driver Loading" New topic
Author

Driver Loading

Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
Hello everybody,

I am creating an application. I know that for database purpose I have to load the drivers, create connection object and make the Statements and PreparedStatement object. But I don't have to load the driver in every page where database access is required. Or I hope I don't have to create Connection Object in every page too, So what should I do for the application to be developed in an efficient manner. Please help me. Please advice me.

Thanking you a lots.


ZORAM<br />"If it's true that we are here to help others,<br />then what exactly are the others here for?"
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Look at a Connection Pool Manager like DBCP
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
The driver only needs to be loaded once, possibly at startup time. For using connections it's customary to use a connection pool in web applications, which your servlet container may provide for you (e.g. Tomcat does).

And, of course, it's considered bad design to do anything DB-related in a JSP. Please consider doing DB access in a backing bean or a proper class.


Ping & DNS - my free Android networking tools app
Amit Kumargupta
Ranch Hand

Joined: Apr 13, 2007
Posts: 54
Use the Connection Pooling
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
Hi Zoram Paul,

One way is that you can go with connection pooling, but for that you need to have is an Application Server.

But I think you just want run JDBC code in which no need to load driver each and every time right?

For that you can use Singleton class which can be used to get Connection object and will also load the driver only once. Like below.





���



..


Use can use this class any where within applcaiton like

..
..
Connection con = ConnectionManager.getConnection(�Driver� , �URL�);

OR like

..
..




Thanks,

Ashok Mor
Ness Technology, Bangalore.

[ July 19, 2007: Message edited by: Ashok Mor ]
[BPSouther: Added code tags]
[ July 19, 2007: Message edited by: Ben Souther ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
Originally posted by Ashok Mor:
One way is that you can go with connection pooling, but for that you need to have is an Application Server.


No application server or servlet container is needed to use a connection pool. They can provide one as part of the server infrastucture (as was pointed out above), but in general, pools are independent of the environment they run in.


This kind of lazy instantiation is not thread-safe, and must not be used in a web application, unless further synchronization measures are used to protect it.
[ July 19, 2007: Message edited by: Ulf Dittmer ]
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
You are right, we need to use zynchronized keyword against these methods....

Servlet container is nothing to do with Connection pooling...Am I right Mr. Ulf Dittmer?



But of course I will be intrested Mr. Ulf Dittmer, that how can we implement Connection Pooling without any server resource like an Application Server?


Where will you create pool to store the the instance of Connection?

Who will be responsible for managing all these operation of connection pool??

_____________
Ashok Mor
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Ashok Mor:

One way is that you can go with connection pooling, but for that you need to have is an Application Server.


This is the JSP forum. It's a given that there will be a server.

Most experienced web developers will rely upon container-managed connection pooling.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

For those who might be interested, one popular JDBC connection pool manager is the dbcp connection pooler available from the Apache Jakarta Commons. It is the default connection pooler for Tomcat and can be used with any Java app that needs a JDBC connection pooler, not just application servers.

dcbp is built on top of other Jakarta Commons - in particular, it uses a generic object pooler to manage the connection objects.

If you're using an application server - and if you're working with JSPs you haven't got much choice - you can expect that it will support a database connection pool.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
Originally posted by Bear Bibeault:


This is the JSP forum. It's a given that there will be a server.

Most experienced web developers will rely upon container-managed connection pooling.


Yes Bear Bibeault, You are right, but for that we need to have a cotianer at server na?

So it might be in ApplicationServer or any other Server.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Don't make use of the colloquial words like "na" in these context Ashok. Please make use of RealWords .



First of all, having the connection instance "public" may totally collapse the purpose as its static as well. Please always have the instance variables "private" in these scenarios. and ensure they are obtained ONLY via the appropriate methods (preferably static).

HtH.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
Originally posted by Raghavan Muthu:
Don't make use of the colloquial words like "na" in these context Ashok. Please make use of RealWords .



First of all, having the connection instance "public" may totally collapse the purpose as its static as well. Please always have the instance variables "private" in these scenarios. and ensure they are obtained ONLY via the appropriate methods (preferably static).

HtH.


Raghavan Muthu, you are right that we should use only infromative words only, But here 'na' is nothing, because in hindi what we say? even though if you have any dictionary to use please let me know.
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
I think we all should better concentrate in providing the solution rather then letting our egos clash. Lets keep our sentence related to technology only.

Regards
Prem Kashyap
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSPs run in servlet containers.
Most, if not all, up to date servlet containers provide connection pooling.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355


I think we all should better concentrate in providing the solution rather then letting our egos clash


I think there is nothing called 'ego' and 'clash' here prem. Just i thought of directing Ashok towards the rules being followed here so that we will be conscious in our further usages right.

Ashok, i also know the meaning of "na". Have you read the link pointing to "UseRealWords"? If any one who reads this post and does not have the knowledge in Hindi (or any other language which we know), they will have a tough time in understanding this. Not just speaking in this "na" context but just to avoid any further usages in future.

Hope this clears and helps!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Originally posted by Ashok Mor:
because in hindi what we say? even though if you have any dictionary to use please let me know.


Please be sure to use real English words when posting to the forums.
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
[removed non related question]
[ July 26, 2007: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Zoram Paul,
Please use this forum for JSP related questions only.
Thanks,
-Ben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Driver Loading