Servlets and object persistence

J

Joe

Hi,

I'm writing a simple persistence layer to be used both in a
servlet-based webapp, and also for command line tools to access the
underlying store (a relational db).

Because of this dual use I want to avoid any mention of
ServletContext, etc in the Persistence layer, but I've run into a
problem.

I need to store some per-transaction information (such as the db
connection from the pool to use for the transaction, a local cache,
etc), but want to avoid passing it around with every call to the layer
I make (I want the layer to be as transparent as possible).

In a servlet application this would typically be achieved using the
context, which can be accessed from anywhere at anytime, but as stated
I'd like to avoid this.

So, my plan was to maintain an application wide hash relating Thread
names to context information.

Is this a reasonable plan? Or madness?

Any help/tips/advice welcome.
 
C

Christophe Vanfleteren

Joe said:
Hi,

I'm writing a simple persistence layer to be used both in a
servlet-based webapp, and also for command line tools to access the
underlying store (a relational db).

Because of this dual use I want to avoid any mention of
ServletContext, etc in the Persistence layer, but I've run into a
problem.

I need to store some per-transaction information (such as the db
connection from the pool to use for the transaction, a local cache,
etc), but want to avoid passing it around with every call to the layer
I make (I want the layer to be as transparent as possible).

In a servlet application this would typically be achieved using the
context, which can be accessed from anywhere at anytime, but as stated
I'd like to avoid this.

So, my plan was to maintain an application wide hash relating Thread
names to context information.

Is this a reasonable plan? Or madness?

Any help/tips/advice welcome.

Read up on ThreadLocal, that allows you to store a separate value/thread.
So all transactions run in the same thread could use the same Threadlocal
without you having to pass it explicitly.

You also might want to check out Spring and Hibernate, which do what you want
to do when you combine them.
 
J

Joe

Christophe Vanfleteren said:
Read up on ThreadLocal, that allows you to store a separate value/thread.
So all transactions run in the same thread could use the same Threadlocal
without you having to pass it explicitly.

You also might want to check out Spring and Hibernate, which do what you want
to do when you combine them.

Excellent! Thanks very much for that - it seems to be just what I need.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top