每个任务不会初始化的单个数据库连接 [英] Single db connection that does not get initialised per task
问题描述
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();
}
}
: Parser
, ItemDispatcher
和数据库
。我使用一个线程池重用固定数量的线程(10)。我收到类 Parser
中的回调 update()
方法的更新。随着更新进来,我创建Item对象,它提交为可执行的任务。从 ItemDispatcher
我将项
传递到数据库
类。我意识到有一个问题。我需要Database类连接到数据库,但由于Threads正在为每个项目调用Database constructo
r,我不想每次都创建连接,而是使用一个连接初始化,从不再。不知道如何做到这一点,有人可以帮助请一些想法和建议吗?我不想让解析器类知道Database类。在哪里可以添加连接和 preparedStatement
代码?
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 constructo
r 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 ?
推荐答案
创建单例DAO(数据访问对象)类,并让此类初始化池。如果你不确定这两个术语,只要问一下,我会为你找到一些代码。
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();
}
}
此代码不是线程安全的,同一连接在每个线程中。如果你需要线程安全(你这样做),你可以使用一个ConnectionPool为你提供线程安全,并将线程数设置为1.BoneCP是一个众所周知的连接池的名称。
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.
这篇关于每个任务不会初始化的单个数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!