问题
public class Parser {
ExecutorService pool = Executors.newFixedThreadPool(10);
public void update() {
Item item = new Item(subj.getName(), dateBuilder.toString(),
cobBuilder.toString(), interest, count);
pool.submit(new ItemDispatcher(item));
}
}
public class ItemDispatcher implements Runnable {
private Item item;
public ItemDispatcher(Item someItem) {
this.item = someItem;
}
@Override
public void run() {
try {
new Database(item).writeToDb();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class Database {
private String name;
private String date;
private String cob;
private String interest;
private String count;
private String _url;
private String _userId;
private String _password;
private String _dbLib;
private String _dbFile;
private Connection _conn;
private PreparedStatement _statement;
public Database(Item item) {
name = item.get_item();
date = item.get_date();
cob = item.get_cob();
interest = item.get_interest();
count = item.get_count();
}
public void writeToDb() {
try {
//statment.setString();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
I have three different classes in package: Parser, ItemDispatcher and Database. I am using a thread pool that reuses a fixed number of threads (10). I receive updates in callback update() method in class Parser. As updates come in I create Item objects which submits as a runnable tasks for execution. From ItemDispatcher I pass the item to the Database class. I've realised there is an issue. I need Database class to connect to database, but as Threads are calling Database constructor for every item, I don't want to create connection everytime, instead use one connection which gets initialised and never again. Not sure how to do this, can someone help please with some ideas and suggestions ? I dont want parser class to know about the Database class. Where can I add the connection and preparedStatement code ?
回答1:
Create a singleton DAO (data access object) class and let this class initialize the pool. If you're unsure about either term, just ask and I'll find some code for you.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ItemDAO {
private static ItemDAO dao;
private Connection conn;
private PreparedStatement prepStat;
private static final String SQL_INSERT_ITEM = "INSERT INTO tbl_item SET id=?`, name = ?, count = ?;";
private ItemDAO() {
conn = null; /* put initialization code for the connection here */
try {
prepStat = conn.prepareStatement(SQL_INSERT_ITEM); // prepare a statement
} catch (SQLException e) {
// What do you want to do if it fails?
e.printStackTrace();
}
}
public static ItemDAO getInstance() {
if (dao == null)
dao = new ItemDAO();
return dao;
}
public void writeItemToDB(Item item) throws SQLException {
prepStat.setInt(1, item.getId());
prepStat.executeUpdate();
}
}
This code is not thread safe but it will use the same connection in each thread. If you need thread safety (you do) you can use a ConnectionPool that gives you thread safety and set the number of threads to 1. "BoneCP" is the name of a well known connection pool.
来源:https://stackoverflow.com/questions/21428512/single-db-connection-that-does-not-get-initialised-per-task