0
public class Database {

    private String ric;
    private String volume;

    private String _url;
    private String _userId;
    private String _password;
    private String _dbLib;
    private String _dbFile;
    private Connection _conn;
    private PreparedStatement _statement;

    public Database(LSE item) {
        ric = item.get_ric();
        volume = item.get_volume();
    }

    public void writeToDb() throws SQLException{
          //setString           
    }
}

I have a ItemDispatcher class:

public class ItemDispatcher implements Runnable {

    private LSE lse;

    public ItemDispatcher(LSE lseItem) {
        this.lse= lseItem;
    }

    @Override
    public void run() {
        try {
            new Database(lse).writeToFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

run() method in ItemDispatcher runs repeatedly. I want to create database connection and prepareStatement in Database class, but doing this on Database class constuctor would create connection many times over. How can I change my design to create connection just once and not over and over again on every execution of run(). I am trying to not do this in any other class and just Database class

M06H
  • 1,675
  • 3
  • 36
  • 76

3 Answers3

0

Use the Singleton pattern . This will allow you to have only one instace of the Database connection. Taking your code as an example, it would be like this :

public class Database {

private String ric;
private String volume;

private String _url;
private String _userId;
private String _password;
private String _dbLib;
private String _dbFile;
private Connection _conn;
private PreparedStatement _statement;
private static final Database INSTANCE;

private Database(LSE item) {
    ric = item.get_ric();
    volume = item.get_volume();
}

public static final Database getInstance(LSE item) {
   if (INSTANCE == null) {
         INSTANCE = new Database(LSE item);
    }

   return INSTANCE;
 }

public void writeToDb() throws SQLException{
      //setString           
}
}

If your application will be using Threads (Concurrency), I suggest you also to prepare your singleton for those situations , see this question

Community
  • 1
  • 1
Leo
  • 1,829
  • 4
  • 27
  • 51
  • Your approach assumes that there's only one instance of ItemDispatcher. – Xavier J Jan 28 '14 at 17:37
  • @codenoire , he can still pass the ItemDispatcher, I am not assuming is just one instance of ItemDispatcher, but one instance of the connection. – Leo Jan 28 '14 at 17:51
  • You could likely not build a scalable application where two instances of a the dispatcher share a single database connection. The second one of them needs to do something like a transaction, you're screwed. A singleton is application-wide. That's actually more than what the OP is asking for - he/she is asking for the run method, within the scope of an ***instance***, to use one database connection. – Xavier J Jan 28 '14 at 18:00
  • well , I don't see where she/he specifies the code will be deployed in a cluster or something, what I see is someone that is starting with java, and knowing Singleton is a must, although as you comment is application-wide. – Leo Jan 28 '14 at 18:12
0

Do it in a static block in class Database

static {

}

But this implies that Connections and Statement will be static and then shared by all instances of Database.

Just as an example from another SO post:

public static final Map<String, String> initials = new HashMap<String, String>();
static {
    initials.put("AEN", "Alfred E. Newman");
    // etc.
}
dbermudez
  • 572
  • 3
  • 9
0

Within the scope of ItemDispatcher, declare private variable X of type Database. You might initialize it in a separate method (best) or in the constructor (might be ok). Use the private variable X instead of creating a new instance in method run

Xavier J
  • 4,326
  • 1
  • 14
  • 25