Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Data exchange happening of two objects  RSS feed

 
sushil grover
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one very serious problem as described below:-

1. First custtomer comes, for that customer one object(obj11) of class1 is created.It calls one method of the other class 2 (obj21) by passing one dataobject(obj31) which contains the data of the customer.
2. At the same time, second customer comes, here also, obj12 of class1 calls object of class2(obj22) by passing one dataobject(obj32)
containing the data of the second customer
3. Now what is happening is the dataobject that i pass is getting changed in between.
there are two cases here:-
1. Either call of the object obj21 returns to the object of class obj12(calling class of obj22)(Is it possible?)
2. Or the method that i am calling of class2 is not synchroized. But there is no data change happen in
the class two method.

Please help. Let me know if more detail required.

Thanks
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is hard to say what is going on without some code. Can you show us the code that is causing the problem? If the code is too large can you make a small, relatively simple example which has the same structure as your code, and which shows the same error?

Providing more detail about the environment might help as well - are you using a Web Application by chance, with Servlets and/or JSPs?
 
sushil grover
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks steve.
Here is one example.

class customerDataObject{
// contains varables having customer details
}

class test{
customerDataObject obj1 = new customerDataObject(added some variables related to customer data);

Validator1 v1 = new Validator1();
l_err = v1.validate(obj1);
l_error_list.add(l_err);
Validator v2 = new Validator2();
v2.validate(obj1);
// after this i call function to insert data in the table.
}

class validator1{
public String validate (customerDataObject obj)

}
class validator2{
public String validate (customerDataObject obj)
}



For every customer one object of test class is created. In that Customer details are put into one dataobject(customerDataObject).
And these details are passed to the validatos to validate the data.what is happening here is data of one custome is intermixing with
the other. But this issue comes only when concurrent access to the system happen.
My above observation is based on the logs only. But i could not simulate this issue. Can anybody has some idea.

Yes i am using web application having JSP/servlets. but only login page is jsp.
Please let me know if more information is required.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you provided isn't enough to say anything.

For web applications, I have seen this a lot when you use an instance variable in a Servlet to hold data when moving from one method to the next, or when you store data in the ServletContext scope. For example, this is bad:


This is because the instance of the Servlet (and the ServletContext scope) is shared between all requests and users. So One user's objectData can be shared with another user's objectData and you get the results you are seeing. The problem doesn't only show up between methods, either, for example using the same objectData at the top of the method and at the bottom of the doGet() method would cause the same problem as long as that objectData variable was declared as an instance variable.

A better bet would be to use all local variables:


This way the values only exist within the method, and can't be shared.

Double check that nothing you are doing looks like this, and that your classes don't use static variables to store instance-specific code. Without seeing more of your code, and a working example it will be hard to analyze further.
 
Billy Tsai
Ranch Hand
Posts: 1306
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the producer consumer pattern
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!