1 Connection for everything

Hi Gurus,I have a problem developing my application. The main requirement is that I MUST use just 1 connnection for all users. So, my question is, what kind of patten should I use so as to control this issue.Thanks in advance
[246 byte] By [gmaggessa] at [2007-10-2 3:08:08]
# 1

> Hi Gurus,

>

> I have a problem developing my application. The main

> requirement is that I MUST use just 1 connnection for

> all users. So, my question is, what kind of patten

> should I use so as to control this issue.

>

> Thanks in advance

I assume you mean 1 DB connection.

A 3-tier (not necessarily EJB) architecture will make this possible. If you expect to have a lot of users, you might want to implement asychronous transactions where possible and use JMS.

dubwaia at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> Hi Gurus,

>

> I have a problem developing my application. The main

> requirement is that I MUST use just 1 connnection for

> all users. So, my question is, what kind of patten

> should I use so as to control this issue.

>

> Thanks in advance

... and instead of a singleton, use a framework ensuring your connection object gets created but once and made available to other objects that need it

Torajiroua at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
You could also consider using a connection pool that only has a size of 1.- Saish
Saisha at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
> You could also consider using a connection pool that> only has a size of 1.> > - SaishIsn't the that the only obvious solution? The OP still needs to have only one instance of this pool, if I understand correctly.
dubwaia at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

> > You could also consider using a connection pool

> that

> > only has a size of 1.

> >

> > - Saish

>

> Isn't the that the only obvious solution? The OP

> still needs to have only one instance of this pool,

> if I understand correctly.

The pool itself is usually a Singleton, isn't it? So, that should be okay. Maybe I am not getting something.

- Saish

Saisha at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
> The pool itself is usually a Singleton, isn't it?> So, that should be okay. Maybe I am not getting> g something.Multiple VMs. We don't really know the enviroment either. It could be fat client or web app or applet. Hard to say.
dubwaia at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
Multiple VMs. We don't really know the enviroment either. It could be fat client or web app or applet. Hard to say.Fair point. :^)- Saish
Saisha at 2007-7-15 21:34:54 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
Try a singleton connection and one statement per user.R. Hollenstein
robinhollensteina at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
Try to use monothread sevlet.
barbywarea at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 10
the point is though:is a connection object thread safe? otherwise there would be a bottleneck problem.
barbywarea at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 11

Hi,

Connection is not thread safe.

If you want to use only a connection you could use a monothread servlet or a pool on 1 connection.

I recommend that you use a pool of 1 connection in order to add more connection to the pool if you have problem.

I think that if you system need you could pay for more connection.

This solution make simple the addtion of connection

barbywarea at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 12
You can use Singleton pattern but iam still wondering why do u need only one Connection? Definitly some problem is there with your design. Thanks,Anshul
Anshul_katocha at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 13

> You can use Singleton pattern but iam still wondering

> why do u need only one Connection? Definitly some

> problem is there with your design.

It not the design, it's a requirement. And again a singleton doesn't necessarily solve this. Singletons are only unique per classloader in Java. If this is a client server app, every use instance has a Singleton instance. That means if there is more than one user using the system, there is more than one connection in use.

You cannot assume this is a web app running in a single VM with a single classloader.

dubwaia at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 14

> You cannot assume this is a web app running in a

> single VM with a single classloader.

i think you worry too much. a client invokes a web function via a servlet. usually if the client sticks to one servlet, there wont be multiple instances of the connection; unless of course you do unusual things such as multiple instances of a servlet...

dubwaia at 2007-7-15 21:34:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 15
you can simply keep the connection object open for the duration of the servlet, and make sure people wait for their turn to use this single object.
at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 16

> > You cannot assume this is a web app running in a

> > single VM with a single classloader.

>

> i think you worry too much. a client invokes a web

> function via a servlet. usually if the client sticks

> to one servlet, there wont be multiple instances of

> the connection; unless of course you do unusual

> things such as multiple instances of a servlet...

Show me where the OP says this is a servlet. And no, it's not unusual to run multple VMs. That's what real enterprises architectures do. That you think this is unusual just reaffirms that you don't really know what you are writing about. Have you ever heard of clustering? You think that large websites with 100,000s of thousands of users have a single VM on a single server?

dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 17
> with> 100,000s of thousands of usersmake that 100s of thousands.
dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 18
> > with 100,000s of thousands of users> > make that 100s of thousands.you have a wild imagination, 100s of 1000s of 1000000s more than i had expected...in that case, using one connection would not be proper anyway.
dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 19
> Show me where the OP says this is a servlet. as long as you are talking about a web application, servlets are there, and all client invocations are done via servlets.
dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 20

> > Show me where the OP says this is a servlet.

>

> as long as you are talking about a web application,

> servlets are there, and all client invocations are

> done via servlets.

I can't recall ever using a servlet in the perl or C web servers that I have worked on.

jschella at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 21
> > Show me where the OP says this is a servlet. > > as long as you are talking about a web application,> servlets are there, and all client invocations are> done via servlets.Show me where the OP says this is a web application.
dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 22

> I can't recall ever using a servlet in the

> perl or C web servers that I have worked on.

i trust that you are telling the truth, and i also trust that you hopefully correctly used whats called cgi scripts for those things at that time. you may argue they are not exactly the same thing, yet from the functional perspective, they served the same purpose, and that is: all client invocations have to go through them.

dubwaia at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 23
Asnchronous JMS is a god idea!
gmaggessa at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 24
No. I can't. I must use a class (it's in a jar file) that is a Singleton and always gives you the same connection, what causes me many problems when I have concurrent users.
gmaggessa at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 25
If many statements share the same connection u'r gonna have problems, right?
gmaggessa at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 26
Oh, Jesus!!!
gmaggessa at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 27
> If many statements share the same connection u'r> gonna have problems, right?Depends on the database, driver and what one does with the statements.
jschella at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 28

> > I can't recall ever using a servlet in the

> > perl or C web servers that I have worked on.

>

> i trust that you are telling the truth, and i also

> trust that you hopefully correctly used whats called

> cgi scripts for those things at that time. you may

> argue they are not exactly the same thing, yet from

> the functional perspective, they served the same

> purpose, and that is: all client invocations have to

> go through them.

Truth about what?

Are you trying to claim that you have used a servlet in C and/or perl? Or that they exist?

And no cgi is not the same as a servlet.

jschella at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 29
Use SingleThreadModel with unit instance as facade to connection factory
cpallaba at 2007-7-20 17:42:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 30

Hi,

I think that you must could one connection.

I think that you must use a Synchonized form. SingleThread Model is very hard, you could make some thing until use the connection.

Synchonized the get of the connection and the use of connection only.

Synchonized()

{

getConnection()

use connection();

}

You must try to Synchonized the minimal of code.

barbywarea at 2007-7-20 17:42:56 > top of Java-index,Other Topics,Patterns & OO Design...
# 31
Assuming the db connection creation and release will be in sync with request acceptance and response send by the Servlet. This way you can load the connection request queuing with the web container. Needless to say the instance should be one.
cpallaba at 2007-7-20 17:42:56 > top of Java-index,Other Topics,Patterns & OO Design...
# 32
Instead you can use a semaphore to connection pool object, just in case try to ensure a single connection used at a time.
cpallaba at 2007-7-20 17:42:56 > top of Java-index,Other Topics,Patterns & OO Design...
# 33
The method getConnection, get The connection of a Pool or Singleton.
barbywarea at 2007-7-20 17:42:56 > top of Java-index,Other Topics,Patterns & OO Design...