开发者

How to create request safe class or class property

开发者 https://www.devze.com 2023-03-07 02:35 出处:网络
I will try to be short and most specific. It is possible to create class, class property or \"something\" which will be created for each request ? I mean something what will not be persistent for whol

I will try to be short and most specific. It is possible to create class, class property or "something" which will be created for each request ? I mean something what will not be persistent for whole container (static class property).

I need to store internal identifier into class or property, which will be diferent for each request and persistent for whole request. I'm using java filters and before doFilter I need to save that identifier "somewhere". Because I need to use that identifier in application for some operations. And after doFilter I need to take that identifier from "somewhere" and update some other things.

Thanks for any advice how to solve this problem ...


Edited 24th May 2011 10:25 am

Ok, I will try to explain my problem closely.

I'm using Tomcat 6 with BlazeDS utils. This server is working as backend for a flex application. So, if I want to send some messages or object or whatever else to server I just call something like this:

service.doSomethingWithThis( data )

In flex is the "service" property mapped to class, eq. "foo.bar.BazService" and this service has method "doSomethingWithThis" with parameter "data". And when request is recieved by server then "doSomethingWithThis" method is executed with parameter "data".

public int doSomethingWithThis( Object data ) {
  GenericDAO genDao = new GenericDao();
  genDao.create( data );
}

So, as you can see, I don't have access to the request property. What I call at Flex side it is called on Java side.

Another problem is with that DAO object (in this case GenericDAO). Those DAO objects are little bit specific. They are asking for database connection and this connection is provided by a singleton Class, which holds database connection for the whole application. So, I'm not passing connection parameter to DAO objects as is usual, but they are asking for that connection themselves. I know that it is not the right approach :).

Now we are trying to fix this problem and we want to use pooling database connection, which is provided by Tomcat. And there is problem. If I retrieve connection I need that connection put back to the pool. But my DAO objects are not closing database connection. Solution can be to pass connection parameter to DAO objects when service method is called and at the end c开发者_运维百科lose that connection, but I have a lot of services and it is a lot of refactoring. So I'm trying to find an another way, when I will be able to encapsulate whole request (Java Filters) and store database connection "somewhere". And my DAO Object can take this connection from "somewhere" place.

Store database connection in request is one solution, but as you can see, I don't know how to access this request in my DAO classes, or service classes. Other way is to use session, but there is also problem. There can be multiple requests to server and first request which will be completed will close that connection. So, other requests can not use that database connection anymore, because it was returned to pool.

So I'm targeting for solution, when I will be able to get one database connection for the whole request.


request.setAttribute("name", variable);

will persist for the lifetime of the request, and no longer.


If I your requests are executing in different threads, you can store your variable in ThreadLocal variable, which has different value for every thread (and for request).

0

精彩评论

暂无评论...
验证码 换一张
取 消