Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to use PreparedStatements on a servlet the most effici..

 
   Database Forums (Home) -> Java RSS
Next:  Convert to Unix Timestamp  
Author Message
mebe

External


Since: Feb 07, 2008
Posts: 2



(Msg. 1) Posted: Thu Feb 07, 2008 2:54 am
Post subject: How to use PreparedStatements on a servlet the most efficient way?
Archived from groups: comp>lang>java>databases (more info?)

Hello,

i wonder whats the most efficient way to create PreparedStatements on
a
servlet. Currently i see the following two options:
1 Creating them in the method at which they are executed. (Seems
obvious)
2 Creating all PreparedStatements in the servlets contructor and store
them
in the servlets class. This way only the parameters must be supplied
if a
PreparedStatement should be executed, and the time to create the
PreparedStatement could be saved.
But is this approach thread save? (What if two identical requests are
issued at the same time? Wouldn't they interfere each other?)

Please let me know if you have any information about this topic.
Thanks in advance!

 >> Stay informed about: How to use PreparedStatements on a servlet the most effici.. 
Back to top
Login to vote
mebe

External


Since: Feb 07, 2008
Posts: 2



(Msg. 2) Posted: Thu Feb 07, 2008 8:21 am
Post subject: Re: How to use PreparedStatements on a servlet the most efficient [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 7 Feb., 15:59, Lew wrote:
> mebe wrote:
> > i [sic] wonder whats the most efficient way to create PreparedStatements on
> > a
> > servlet. Currently i see the following two options:
> > 1 Creating them in the method at which they are executed. (Seems
> > obvious)
>
> And pretty much necessary. Although strictly speaking they don't have to be
> created in the method necessarily, they do have to have carefully controlled
> lifespans.
>
> > 2 Creating all PreparedStatements in the servlets contructor and store
> > them
> > in the servlets class. This way only the parameters must be supplied
> > if a
> > PreparedStatement should be executed, and the time to create the
> > PreparedStatement could be saved.
> > But is this approach thread save? (What if two identical requests are
> > issued at the same time? Wouldn't they interfere each other?)
>
> Never mind thread-safe, it's not even safe in a single thread, unless you take
> the proper care.
>
> PreparedStatements are tied to the connection that created them. In order to
> keep PreparedStatements around you have to keep their connections open. Since
> connections are usually a relatively scarce resource that is a problem. Of
> course, one connection can support many Statements, so you could manage that
> by jamming all the app's activity through a single connection, or a small set
> of them, but that could be a bottleneck itself.
>
> If you have a ResultSet open from a PreparedStatement and you re-execute the
> Statement, you lose that ResultSet - it closes. (Disconnected RowSets are an
> exception, I think.)
>
> > A ResultSet object is automatically closed when the Statement object
> > that generated it is closed, re-executed, or used to retrieve the next
> > result from a sequence of multiple results.
>
> <http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html>
>
> That means you have to make darn sure that the PreparedStatement is not
> re-used by one block of code while another still has a ResultSet open from it.
>
> Which further implies that if you have two clients who need to execute the
> same query concurrently, they cannot share a PreparedStatement; you will need
> two. Generalize to /n/ for /n/ concurrent queries. If you want all of them
> to be kept open, then you'll need /n/ PreparedStatements of each SQL command
> or query at all times, even if the usual load is far smaller than /n/
> concurrent clients.
>
> There is an intermediate approach, much like a thread pool or a connection
> pool a PreparedStatement pool of limited lifespan might be useful under the
> right circumstances, but it's a lot of work. It also could increase fragility
> of the system, a mighty price to pay for a few milliseconds.
>
> Another intermediate approach might be to create a connection, or a primary
> connection, for each session, and create the most commonly-used
> PreparedStatements off that connection to service that one session. You still
> have to be careful that that session doesn't use one of its PreparedStatements
> while it still has an active ResultSet on it.
>
> There's a lot of work managing PreparedStatements if you go beyond the default
> "create'em when you need'em" approach. It could be worth it if the
> PreparedStatement overhead justifies it, but it's not an optimization you'd
> want to use early in development while getting the essential logic correct.
> Build it right first, then speed it up. (Didn't Jon Bentley coin, "Make it
> right, then make it fast" in his /Programming Pearls/ column? Even if not, he
> sure put a lot of other wisdom in there.) Once you've gotten the logic right,
> use actual metrics to determine what needs speed.
>
> Is PreparedStatement creation a huge bottleneck in your application? What do
> your measurements tell you? Please share those results with us.
>
> --
> Lew

Wow, i am impressed!
Lew, thank you verry much for the information.
I don't have any performance problems yet, but i just started coding
the servlet and i wanted to be sure to use the best approach.
Considering what you sayed about ResultSets, i realize that it was a
foolish idea - and so i am even more thankful for your detailed
explanation.

 >> Stay informed about: How to use PreparedStatements on a servlet the most effici.. 
Back to top
Login to vote
Lew

External


Since: Aug 30, 2007
Posts: 57



(Msg. 3) Posted: Thu Feb 07, 2008 9:59 am
Post subject: Re: How to use PreparedStatements on a servlet the most efficient [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

mebe wrote:
> i [sic] wonder whats the most efficient way to create PreparedStatements on
> a
> servlet. Currently i see the following two options:
> 1 Creating them in the method at which they are executed. (Seems
> obvious)

And pretty much necessary. Although strictly speaking they don't have to be
created in the method necessarily, they do have to have carefully controlled
lifespans.

> 2 Creating all PreparedStatements in the servlets contructor and store
> them
> in the servlets class. This way only the parameters must be supplied
> if a
> PreparedStatement should be executed, and the time to create the
> PreparedStatement could be saved.
> But is this approach thread save? (What if two identical requests are
> issued at the same time? Wouldn't they interfere each other?)

Never mind thread-safe, it's not even safe in a single thread, unless you take
the proper care.

PreparedStatements are tied to the connection that created them. In order to
keep PreparedStatements around you have to keep their connections open. Since
connections are usually a relatively scarce resource that is a problem. Of
course, one connection can support many Statements, so you could manage that
by jamming all the app's activity through a single connection, or a small set
of them, but that could be a bottleneck itself.

If you have a ResultSet open from a PreparedStatement and you re-execute the
Statement, you lose that ResultSet - it closes. (Disconnected RowSets are an
exception, I think.)

> A ResultSet object is automatically closed when the Statement object
> that generated it is closed, re-executed, or used to retrieve the next
> result from a sequence of multiple results.
<http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html>

That means you have to make darn sure that the PreparedStatement is not
re-used by one block of code while another still has a ResultSet open from it.

Which further implies that if you have two clients who need to execute the
same query concurrently, they cannot share a PreparedStatement; you will need
two. Generalize to /n/ for /n/ concurrent queries. If you want all of them
to be kept open, then you'll need /n/ PreparedStatements of each SQL command
or query at all times, even if the usual load is far smaller than /n/
concurrent clients.

There is an intermediate approach, much like a thread pool or a connection
pool a PreparedStatement pool of limited lifespan might be useful under the
right circumstances, but it's a lot of work. It also could increase fragility
of the system, a mighty price to pay for a few milliseconds.

Another intermediate approach might be to create a connection, or a primary
connection, for each session, and create the most commonly-used
PreparedStatements off that connection to service that one session. You still
have to be careful that that session doesn't use one of its PreparedStatements
while it still has an active ResultSet on it.

There's a lot of work managing PreparedStatements if you go beyond the default
"create'em when you need'em" approach. It could be worth it if the
PreparedStatement overhead justifies it, but it's not an optimization you'd
want to use early in development while getting the essential logic correct.
Build it right first, then speed it up. (Didn't Jon Bentley coin, "Make it
right, then make it fast" in his /Programming Pearls/ column? Even if not, he
sure put a lot of other wisdom in there.) Once you've gotten the logic right,
use actual metrics to determine what needs speed.

Is PreparedStatement creation a huge bottleneck in your application? What do
your measurements tell you? Please share those results with us.

--
Lew
 >> Stay informed about: How to use PreparedStatements on a servlet the most effici.. 
Back to top
Login to vote
efriednospam

External


Since: Jan 04, 2005
Posts: 12



(Msg. 4) Posted: Fri Feb 08, 2008 3:01 am
Post subject: Re: How to use PreparedStatements on a servlet the most efficient [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article , Lew wrote:
>mebe wrote:
>> i [sic] wonder whats the most efficient way to create PreparedStatements on
>> a
>> servlet. Currently i see the following two options:
>> 1 Creating them in the method at which they are executed. (Seems
>> obvious)
>
>And pretty much necessary. Although strictly speaking they don't have to be
>created in the method necessarily, they do have to have carefully controlled
>lifespans.
>
>> 2 Creating all PreparedStatements in the servlets contructor and store
>> them
>> in the servlets class. This way only the parameters must be supplied
>> if a
>> PreparedStatement should be executed, and the time to create the
>> PreparedStatement could be saved.
>> But is this approach thread save? (What if two identical requests are
>> issued at the same time? Wouldn't they interfere each other?)
>
>Never mind thread-safe, it's not even safe in a single thread, unless you take
>the proper care.
>
>PreparedStatements are tied to the connection that created them. In order to
>keep PreparedStatements around you have to keep their connections open. Since
>connections are usually a relatively scarce resource that is a problem. Of
>course, one connection can support many Statements, so you could manage that
>by jamming all the app's activity through a single connection, or a small set
>of them, but that could be a bottleneck itself.
>
>If you have a ResultSet open from a PreparedStatement and you re-execute the
>Statement, you lose that ResultSet - it closes. (Disconnected RowSets are an
>exception, I think.)
>
>> A ResultSet object is automatically closed when the Statement object
>> that generated it is closed, re-executed, or used to retrieve the next
>> result from a sequence of multiple results.
><http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html>
>
>That means you have to make darn sure that the PreparedStatement is not
>re-used by one block of code while another still has a ResultSet open from it.
>
>Which further implies that if you have two clients who need to execute the
>same query concurrently, they cannot share a PreparedStatement; you will need
>two. Generalize to /n/ for /n/ concurrent queries. If you want all of them
>to be kept open, then you'll need /n/ PreparedStatements of each SQL command
>or query at all times, even if the usual load is far smaller than /n/
>concurrent clients.
>
>There is an intermediate approach, much like a thread pool or a connection
>pool a PreparedStatement pool of limited lifespan might be useful under the
>right circumstances, but it's a lot of work. It also could increase fragility
>of the system, a mighty price to pay for a few milliseconds.
>
>Another intermediate approach might be to create a connection, or a primary
>connection, for each session, and create the most commonly-used
>PreparedStatements off that connection to service that one session. You still
>have to be careful that that session doesn't use one of its PreparedStatements
>while it still has an active ResultSet on it.
>
>There's a lot of work managing PreparedStatements if you go beyond the default
>"create'em when you need'em" approach. It could be worth it if the
>PreparedStatement overhead justifies it, but it's not an optimization you'd
>want to use early in development while getting the essential logic correct.
>Build it right first, then speed it up. (Didn't Jon Bentley coin, "Make it
>right, then make it fast" in his /Programming Pearls/ column? Even if not, he
>sure put a lot of other wisdom in there.) Once you've gotten the logic right,
>use actual metrics to determine what needs speed.
>
>Is PreparedStatement creation a huge bottleneck in your application? What do
>your measurements tell you? Please share those results with us.
>
>Lew

Gread advice.

The bulk of the work in creating a PreparedStatement is done by the database,
and any decent modern database will cache them. There shouldn't be much
overhead, but as Lew suggested, you need metrics.

Eric
 >> Stay informed about: How to use PreparedStatements on a servlet the most effici.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
QueryDataSet in servlet - Hi, Here is what I am trying to do: In servlet: 1. Get queries from an applet. 2. Do the queries against SQL Server and return a QueryDataSet to the Applet. In Applet: 1. Request a QueryDataSet. 2. Display QueryDataSet in a JdbTable. (Only show rows,...

DBCP + Servlet in Tomcat - Hi everyone, I am working Servlet and DBCP in Tomcat 5.5.15. Are there any methods or tools that can prove DBCP working properly and have performance gain on making DB connections? Thanks! s.a.

Problem with servlet and SQL Server getConnection() - I am trying to connect to SQL server from a servlet running in Tomcat 4.1. The following is the code : try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:dpt","sa",&qu...

2nd Globals Programming Challenge - USD $3,500 Grand Prize.. - Want to show how good you are at coding "Big Data" solutions? InterSystems Corp. is hosting a series of programming challenges in the Globals Community. Our 2nd Globals Challenge kicks off with a two day competition on Friday, December 02, 20...

algorithm for generating top fuzzy variations ... - Hello all, I am interested in obtaining the top N fuzzy variations of an string (a person or company name) using the same concept as the Levenshtein distance. Ussually Levenshtein is used to compute the distance between two given strings ... but I would...
   Database Forums (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]