title: Where (exactly) should i use synchronization in my Java EE application
body: <p>I am developing a content management system for game reviews using Java EE. </p>
  
  <p>I have a very simple question and here it is:</p>
  
  <p>I have an ArrayList with Game objects. Every game object has GameRank attribute which is a simple class defined like this:</p>
  
  <pre><code>public class GameRank 
  {
      private int activity;
      private int positiveVotes;
      private int negativeVotes;
  
  
       public GameRank(int activity, int positiveVotes, int negativeVotes) 
       {
         this.activity = activity;
         this.positiveVotes = positiveVotes;
         this.negativeVotes = negativeVotes;   
       }
  }
  </code></pre>
  
  <p>Visitors of the web site will have an option to vote positive or negative about the game and the result will be sent to the server using ajax for example. </p>
  
  <p>So the question is:</p>
  
  <p>Where should i synchronize the access to the attributes of the GameRank objects - their getter and setter methods OR in the Controller Servlet that handles the users votes and based on the game id desides which object should be updated?</p>
  
  <p>10x in advance</p>
  
  <hr>
  
  <p>If i decide to use the synchronization inside the class i can use AtomicInteger as one of the posters suggested or this:</p>
  
  <pre><code>public class GameRank 
  {
  private volatile int activity;
  private volatile int positiveVotes;
  private volatile int negativeVotes;
  
  
  public GameRank(int activity, int positiveVotes, int negativeVotes) 
  {
      this.activity = activity;
      this.positiveVotes = positiveVotes;
      this.negativeVotes = negativeVotes;
  
      this.checkAndFixValues();
  }
  
  private void checkAndFixValues()
  {
      if(this.activity &lt; 1) this.activity = 1;
      if(this.positiveVotes &lt; 1) this.positiveVotes = 1;
      if(this.negativeVotes &lt; 1) this.negativeVotes = 1;
  }
  
  public int getActivity() 
  {
      synchronized(GameRank.class)
      {
          return activity;
      }
  }
  
  public int getPositiveVotes() 
  {
      synchronized(GameRank.class)
      {
          return positiveVotes;
      }
  }
  
  public int getNegativeVotes() 
  {
      synchronized(GameRank.class)
      {
          return negativeVotes;
      }
  }
  
  public void incrementActivitiy()
  {
      synchronized(GameRank.class)
      {
          activity++;
      }
  }
  
  }
  </code></pre>
  
  <p>Am i right?</p>