私はdatabaseQueue.add(message);
を経由してキューにデータを送信するソケット接続を持っています。次に、DatabaseProcessor
クラスは、開始時にスレッドとして開始され、単一のデータベース接続が行われます。接続はdatabaseQueue.take();
でメッセージを受け取り、処理します。この部分の良い点は、データベース接続が1つしかないことです。突発的にデータが急増しているときに問題が発生します。もう一つの方法は、受け取った各データに対して、私はメソッドを開いて閉じます。ここに行く最善の方法である重い荷物のためのあなたの経験に基づいていますか?キュー方式または個別DB接続?
私のコードの一部抜粋。
class ConnectionHandler implements Runnable {
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
// gets data from an inbound connection and queues it for databse update
public void run() {
databaseQueue.add(message); // put to db queue
}
}
class DatabaseProcessor implements Runnable {
public void run()
{
// open database connection
createConnection();
while (true)
{
message = databaseQueue.take(); // keep taking message from the queue add by connectionhandler and here I will have a number of queries to run in terms of select,insert and updates.
}
}
void createConnection()
{
System.out.println("Crerate Connection");
connCreated = new Date();
try
{
dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?"+"user=user1&password=*******");
dbconn.setAutoCommit(false);
}
catch(Throwable ex)
{
ex.printStackTrace(System.out);
}
}
}
public void main()
{
new Thread(new DatabaseProcessor()).start(); //calls the DatabaseProcessor
//initiate the socket
}
いいえ、私は私が今までのメッセージを受信し続けるとDBのものを扱う別の別のスレッドに渡すために残っている主な経由でスレッドを1つ作成し、それぞれの新しいメッセージのための新しいスレッドを作成しないでください。だから私はまだ同期が必要なのですか? – user837306