lundi 22 janvier 2018

Single class file on server memory using Spring causing problems

I don't quite know how to explain the situation, I will try to be as clear as possible.

I am currently writing a web-application, using Spring to manage the beans. Obviously, more than one people will use this application. Each user has a set of data related to himself. My problem comes with some poor design I introduced when I just entered the development field. Here is the case:

@Component
public class ServiceClass implements IService    {

@Autowired
private Dependency   firstDependency;

@Autowired
private UsefulObject secondDependency;

private DataSet      dataSet; // THIS LINE IS IMPORTANT

public void entryPoint(String arg1, int arg2, Structure arg3)    {
    /* Query data from a database specific from the project (not SQL
    oriented. I absolutely need this information to keep going. */
    dataSet = gatherDataSet(String ar1);

    /* Treat the data */
    subMethodOne(arg1);
    subMethodTwo(arg2);
    subMethodThree(arg3);
}

private subMethodOne(String arg1)    {
    // Do some things with arg1, whatever
    subSubMethod(arg1);
}

private subSubMethod(String arg1)    {
    /* Use the DataSet previously gathered */
    dataSet.whateverDoing();
}

... // Functions calling sub-methods, using the DataSet;

As every user would have a different dataSet, I thought it would be good to call it at the beginning of every call to my service. In the same way, as is it used very deep in the call hierarchy, I thought it would be a good idea to store it as an attribute.

The problem I encounter is that, when two users are going through this service nearly simultaneously, I have a cross-data issue. The following happens:

  • First user comes in, calls gatherDataSet.
  • Second user comes in, calls gatherDataSet. First user is still treating !
  • First user still uses the dataSet object, which was overrid by Second user.

Basically, the data first user makes use of become false, because he uses data from the second user, which came in short after him.

My questions are the following:

  • Are there design pattern / methods to avoid this kind of behavior ?
  • Can you configure Spring so that he uses two instances fo two users (and so on), to avoid this kinf od problems ?

Bonus: (Kind of unrelated) How to implement a very large data mapper ?

Aucun commentaire:

Enregistrer un commentaire