title: race condition in my java mail web client,while multiple account acssess it
body: <p>I'm trying to build web based email client using javamail api.but I'm facing race condition issues while multiple users trying to accesses their inbox.</p>
  
  <p>my connection class looks like this
  ++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <pre><code>public IMAPConnection(String host, String username, String password) throws NoSuchProviderException {
       synchronized (this) {
          this.username = username;
          this.password = password;
          this.host = host;
          Properties properties = System.getProperties();
          properties.setProperty("mail.imaps.ssl.trust", host);
          properties.setProperty("mail.imap.partialfetch", "false");
          properties.setProperty("mail.imaps.partialfetch", "false");
          properties.setProperty("mail.mime.base64.ignoreerrors", "true");
          session = Session.getDefaultInstance(properties);
          IMAPConnection.store=session.getStore(isSSLEnabled ? imapProtocol : "imap");
       }  
  }
  
  public synchronized boolean DagooIMAPConnection() throws NoSuchProviderException, MessagingException {
      boolean connected = false;
      try {
  
          store.connect(host, username, password);
          connected = true;
      } catch (MessagingException ex) {
          connected = false;
      }
      return connected;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>+++++++++++++++++++++++++++++++++++++++
  and the imap fetching class</p>
  
  <p>++++++++++++++++++++++++++</p>
  
  <pre class="lang-java prettyprint-override"><code>public class GetUserMail {
      private static String host = ReadConfigPropertiesFile.getPropertyValue("geezmail.host");
      private static int itemsPerPage = Integer.parseInt(ReadConfigPropertiesFile.getPropertyValue("emails.perpage"));
      private static Message[] message = null;
      private static Message singlemessage = null;
      private int totalEmails = 0;
      private int newMsgCnt = 0;
      private int newspamCnt = 0;
      private int start = 0;
      private int end = 0;
      public Folder folder = null;
      private String mailFolder;
      public static IMAPConnection con;
  
      public GetUserMail(String username, String password, String mailFolder) throws NoSuchProviderException, MessagingException {
              this.mailFolder = mailFolder;
              con = new IMAPConnection(host, username, password);
      }
  
      public synchronized Message[] GetUserSelectedMail() throws NoSuchProviderException, MessagingException {
          Message[] message2 = null;
  
          if (con.DagooIMAPConnection()) {
              try {
                  folder = con.store.getFolder(mailFolder);
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_ONLY);
                  }
                  end = folder.getMessageCount();
                  start = end - itemsPerPage;
                  if (end &gt;= itemsPerPage) {
                      message2 = folder.getMessages(start + 1, end);
                  } else {
                      message2 = folder.getMessages();
                  }
              } catch (MessagingException ex) {
                  System.out.println("Internal Server Erro: 500");
              }
              //order messages in reverse - from newest to oldest
              int k = message2.length - 1;
              message = new Message[message2.length];
              for (int j = 0; j &lt; message2.length; j++, k--) {
                  message[j] = message2[k];
              }
  
          } else {
              System.out.println("Couldnt connect to server");
          }
          return message;
      }
  
      public synchronized Message[] GetCurserSelectedMail(int start1) throws NoSuchProviderException, MessagingException {
          Message[] message2 = null;
  
          if (con.DagooIMAPConnection()) {
              try {
                  folder = con.store.getFolder(mailFolder);
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_ONLY);
                  }
                  end = folder.getMessageCount();
                  if (end &gt;= itemsPerPage) {
                      message2 = folder.getMessages(start1 + 1, end);
                  } else {
                      message2 = folder.getMessages();
                  }
              } catch (MessagingException ex) {
                  System.out.println("Internal Server Erro: 500");
              }
              //order messages in reverse - from newest to oldest
              int k = message2.length - 1;
              message = new Message[message2.length];
              for (int j = 0; j &lt; message2.length; j++, k--) {
                  message[j] = message2[k];
              }
  
          } else {
              System.out.println("Couldnt connect to server");
          }
          return message;
      }
  
      public synchronized Message GetUserMailOrdered(int UID) throws NoSuchProviderException, MessagingException {
          try {
              if (con.DagooIMAPConnection()) {
                  folder = con.store.getFolder(mailFolder);
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_WRITE);
                  }
                  singlemessage = folder.getMessage(UID);
              } else {
                  System.out.println("Couldnt connect to server");
              }
          } catch (MessagingException ex) {
              System.out.println("Internal Server Erro: 500");
          }
  
          return singlemessage;
      }
  
      public synchronized int GetNewMessageCount() {
  
          try {
              if (con.DagooIMAPConnection()) {
  
                  folder = con.store.getFolder(mailFolder);
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_WRITE);
                  }
                  newMsgCnt = folder.getUnreadMessageCount();
              } else {
                  System.out.println("Couldnt connect to database");
              }
          } catch (Exception ex) {
              System.out.println(ex.toString());
          }
  
          return newMsgCnt;
      }
  
      public synchronized int GetNewMessageCountspm() {
          try {
              if (con.DagooIMAPConnection()) {
                  folder = con.store.getFolder("Spam");
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_WRITE);
                  }
                  newspamCnt = folder.getUnreadMessageCount();
              } else {
                  System.out.println("Couldnt connect to database");
              }
          } catch (Exception ex) {
              System.out.println(ex.toString());
          }
  
          return newspamCnt;
      }
  
      public synchronized int GetTotalEmailsCount() {
          try {
              if (con.DagooIMAPConnection()) {
                  folder = con.store.getFolder(mailFolder);
                  if (!folder.exists()) {
                      folder.create(1);
                  }
                  if (!folder.isOpen()) {
                      folder.open(Folder.READ_WRITE);
                  }
                  totalEmails = folder.getMessageCount();
              } else {
                  System.out.println("Couldnt connect to database");
              }
          } catch (Exception ex) {
              System.out.println(ex.toString());
          }
          return totalEmails;
      }
  }
  </code></pre>