wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Front Controller pattern and DataSource / Connection sharing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Front Controller pattern and DataSource / Connection sharing" Watch "Front Controller pattern and DataSource / Connection sharing" New topic
Author

Front Controller pattern and DataSource / Connection sharing

Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
I've been happily building/using my own basic framework based on the Front Contorller pattern with Command and Controller strategy.
For DB actions, a Connection, which is gotten from a DataSource (pooled connections) is handed to each command that a factory creates, called within the Controller's service method. This seems safe and efficient to me as the connection is used only by that command's invocation, and it's serviced by its own thread.
I've recently seen a similar Command/Controller framework a well-known book that actually creates a 'Connection' at servlet init time in the Controller and then passes that same Connection to each command.
My question is this: Isn't that approach non thread-safe? The controller servlet's Connection instance can't be safely shared by multiple service threads.
What's nagging at me is that it appears to be such and obvious, fundamental problem that I'm wondering if I'm just missing the point - not the author.
Any insights?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Two words:
Connection Pool


Customer surveys are for companies who didn't pay proper attention to begin with.
John Carnell
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 71
Originally posted by Ken Pelletier:
I've been happily building/using my own basic framework based on the Front Contorller pattern with Command and Controller strategy.
For DB actions, a Connection, which is gotten from a DataSource (pooled connections) is handed to each command that a factory creates, called within the Controller's service method. This seems safe and efficient to me as the connection is used only by that command's invocation, and it's serviced by its own thread.
I've recently seen a similar Command/Controller framework a well-known book that actually creates a 'Connection' at servlet init time in the Controller and then passes that same Connection to each command.
My question is this: Isn't that approach non thread-safe? The controller servlet's Connection instance can't be safely shared by multiple service threads.
What's nagging at me is that it appears to be such and obvious, fundamental problem that I'm wondering if I'm just missing the point - not the author.
Any insights?


This is not a very good thing. Any one of the threads in the servlet could close the connection even if another thread is using the connection. In addition, the transaction context in a non-J2EE environment is going to be tied to the connection. If one of the threads commits or rollsback on the connection all database transactions from that connection will commit or rollback.
Thanks,
John


John Carnell<br />Principal Architect<br /> <br />Netchange, LLC<br />1161 HillCrest Heights<br />Green Bay, WI 54313<br /> <br />john.carnell@netchange.us<br /> <br /> <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/159059228X/ref=jranch-20" target="_blank" rel="nofollow">Pro Jakarta Struts, Second Edition</a>
John Carnell
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 71
Tim's comment is correct. If you are using a data source and connection pooling, you should not have any problems. However, if you are creating a connection directly (and not using connection pooling) in the init() and then passing that connection around to each incoming request, you are asking for problems.
Thanks,
John
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
Hi and thanks for the replies. I guess I wasn't as clear as I thought I was. (familiar refrain)
In my framework, I *am* using pooled connections, grabbing one per request, closing after. I'm happy that this is thread-safe, etc.
What this 'best practices' framework from the book does is create a 'Connection' instance from it's DataSource *at servlet init time*. ie: one Connection instance is held for the life of the FrontController servlet. That same connection instance is just getting passed into each Command. It gets closed at servlet destroy() time.
So, totally broken, right? That's what I thought, but I almost didn't believe it made it past editing, so I wondered if I was missing something there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Front Controller pattern and DataSource / Connection sharing
 
Similar Threads
Form Beans necessary?
Front Man 1.5.0
why controller is servlet
Simple Command MVC webapp
Is there anybody pass the exam with using Struts