title: Synchronized Database Access
body: <p>I am using database in my application, I have requirement to access database synchronously, for example when my application starts two threads are accessing the database, how can I avoid  this situation?</p>
  
  <pre><code>private synchronized Database accessDB()
  {
      Database dbObj = null;
      try {
  _uri = URI.create(dbLocation + DB_NAME);
  try {
  dbObj = DatabaseFactory.openOrCreate(_uri, new DatabaseSecurityOptions(false)); 
  System.out.println("Database Created"+_dbTopNews);
      } catch (ControlledAccessException e) {
              // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (DatabaseIOException e) {
  // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (DatabasePathException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  } catch (IllegalArgumentException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
      } catch (MalformedURIException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return dbObj;
  }
  </code></pre>
  
  <p>te above method when debugging returns object to first thread, but it is returning null when  second thread is accessed</p>
  
  <p>A Y </p>