Не знаю многих нюансов в Java. Интересует, можно ли создать одно соединение с mysql и работать с ним в потоках, или в каждом потоке, нужно создавать дополнительный экземпляр соединения? Более 30 потоков, приводит к ошибке mysql: Too many connections. Снял ограничение mysql, такким образом:
set global max_connections = 200;
Но это не выход и хотелось бы знать на будущие, как правильно. Вот код:
public class ParserMain {
public static void main(String[] args) throws java.io.IOException {
// Общее число сообщений
int total_entries = 12000;
// Кол-во потоков
int thread_count = 25;
// Кол-во записей на поток
int entries_thread = total_entries / thread_count;
// Начальный инкремент потока
int thread_end;
// Конечный инкремент потока
int thread_start;
for (int i = 1; i <= thread_count; ++i) {
thread_end = i * entries_thread;
thread_start = thread_end - entries_thread;
new ParserThread(thread_start, thread_end).start();
}
}
}
public class ParserThread extends Thread {
private int jstart;
private int jend;
protected Connection conn;
protected PreparedStatement ps;
protected Statement st;
protected ResultSet rs;
public ParserThread(int jstart, int jend) {
this.jstart = jstart;
this.jend = jend;
}
public void run() {
String dbDriver = "org.gjt.mm.mysql.Driver";
String dbHost = "jdbc:mysql://localhost/test?autoReconnect=true";
try {
Class.forName(dbDriver);
conn = DriverManager.getConnection(dbHost, "root", "");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}