Static Methods in DAO

J

Josh

Google Groups -

I've read conflicting opinions on the use of static methods in DAO
objects. Proponents of the static approach state that you save memory
overhead by eliminating multiple instantiations. Proponents of
instantiate state that the Java DAO pattern indicates non-static
methods and that using static methods makes polymorphism impossible.

My question is this: if all of the DAO methods are static, what
happens when hundreds of simultaneous requests come in. Assuming that
the DAO's have no state, there isn't a possibility of data corruption
(i.e. it's thread safe). However, it seems like all of the threads
would have to shift back/forth for control of the method. Doesn't
this create a throughput bottleneck if every thread shares the same
method?

Maybe an example can better illustrate. Assume that 3 HTTP requests
have come in and each resulted in a call to retrieveRecord, each
passing it's own unique ID. If I understand correctly, all three
threads will be hitting the shared method at the same time. Though it
is "thread safe", won't each thread have to wait for the others to
stop using it until it can?

public SomeDAO {

public static Record retrieveRecord(int id) {
...
}

}

I'm pretty ignorant regarding multithreading, so this could be way
off, but in my mind, all incoming threads (e.g. from HTTP requests)
would share the same static method and couldn't all use it
simultaneously. I know that synchronizing a method definitely results
in this behavior - my question is, does declaring a method static have
a similar effect?

Thanks,
Josh
 
D

David Hilsee

Josh said:
Google Groups -

I've read conflicting opinions on the use of static methods in DAO
objects. Proponents of the static approach state that you save memory
overhead by eliminating multiple instantiations. Proponents of
instantiate state that the Java DAO pattern indicates non-static
methods and that using static methods makes polymorphism impossible.

I'm not even sure that a collection of static methods within a class
qualifies as a DAO. How can it be called a DAO if there is no object
instance? I guess then it would be a DAC (Data Access Class). Maybe I'm
splitting hairs here.

I'm not sure how much memory one saves by using static methods, but I'm
willing to bet that it's not a whole lot, unless there is a DAO assigned to
each client. If DAOs are not created for clients, then it would be
pointless to avoid instantiating them. That would be like a millionaire
making decisions based on mere pennies. To keep the number of instances of
the DAOs down, you could write a factory/singleton compbination that ensures
that there is only one instance of the DAO created. That approach would
mean that it shouldn't contain state, but it does allow for polymorphism.
My question is this: if all of the DAO methods are static, what
happens when hundreds of simultaneous requests come in. Assuming that
the DAO's have no state, there isn't a possibility of data corruption
(i.e. it's thread safe). However, it seems like all of the threads
would have to shift back/forth for control of the method. Doesn't
this create a throughput bottleneck if every thread shares the same
method?

Maybe an example can better illustrate. Assume that 3 HTTP requests
have come in and each resulted in a call to retrieveRecord, each
passing it's own unique ID. If I understand correctly, all three
threads will be hitting the shared method at the same time. Though it
is "thread safe", won't each thread have to wait for the others to
stop using it until it can?

public SomeDAO {

public static Record retrieveRecord(int id) {
...
}

}

I'm pretty ignorant regarding multithreading, so this could be way
off, but in my mind, all incoming threads (e.g. from HTTP requests)
would share the same static method and couldn't all use it
simultaneously. I know that synchronizing a method definitely results
in this behavior - my question is, does declaring a method static have
a similar effect?

A bottleneck would only occur if the static methods were synchronized.
Multiple threads may invoke any method that is not marked as synchronized,
whether it is static or not. In the above code, there would be no
bottleneck.
 
J

Josh Martin

Thank you for the answer. Quick question - if there are shared static
methods, or a single shared object (i.e. singleton), how can multiple
threads make use of the same method simultaneously? Again, apologies
for being a bit uninformed regarding threading, but in my mind, the
only way to allow multiple threads simultaneous access to methods
would be to have more than one instance of them. Otherwise, it seems
like all threads would be vying for the same method - though it would
certainly work.

I think the answer to this question would probably clarify things for
me: Is it possible for multiple threads to execute the same line of
code in a method at the exact same time if there is one shared object
(singleton) or it is a static method? Thanks again!
 
D

David Hilsee

Josh Martin said:
Thank you for the answer. Quick question - if there are shared static
methods, or a single shared object (i.e. singleton), how can multiple
threads make use of the same method simultaneously? Again, apologies
for being a bit uninformed regarding threading, but in my mind, the
only way to allow multiple threads simultaneous access to methods
would be to have more than one instance of them. Otherwise, it seems
like all threads would be vying for the same method - though it would
certainly work.

I think the answer to this question would probably clarify things for
me: Is it possible for multiple threads to execute the same line of
code in a method at the exact same time if there is one shared object
(singleton) or it is a static method? Thanks again!

Assuming that there is no synchronization, the answer is yes.

If you're having some trouble wrapping your head around multithreading, then
you may want to look at some tutorials on it. Sun has a good one here:
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top