N
N
nikesport2014-11-18 14:11:28
Java
nikesport, 2014-11-18 14:11:28

How is jdbc connection implemented in multithreading?

There is a server_db_connection class that returns only one connection, and there is a check_stream class that calls, and there is another class that starts a thread new Thread (new check_stream()).start(); The question is whether in all threads there will be one instance of connection for a given implementation, or is it different in each thread?
public class server_db_connection {
private static String url = "jdbc:mysql://localhost:3306/upomsk";
private static String password = "ravil";
private static String root = "ravil";
private static driver;
private static Connection connection = null;
public static Connection ServerDB_Connection() {
if (connection == null) {
try {
driver = new FabricMySQLDriver();
DriverManager.registerDriver(driver);
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, root, password);
System.out.print("Connection was successful");
} catch (SQLException e) {
System.out.print("Connection failed");
}
}
return connection;
}
}
public check_stream(Socket socket) throws IOException, ClassNotFoundException {
Connection = new server_db_connection.server_db_connection();
while (true) {
try {
switch_key();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nikolai, 2014-11-18
@j_wayne

Depends on the implementation of the jdbc driver, so check its documentation.
Drivers are often thread-safe, but you should not rely on this (it is better to rely on the documentation).
It is very correct in the case of multiple threads to use a connection pool.

A
Alexey Cheremisin, 2014-11-19
@leahch

Of course, one for all threads, but your connection is described as static. If you need a connection to a thread, then use ThreadLocal. And it’s even better to additionally use a connection pool like c3p0 or dbcp (I don’t know which of them is more fashionable now, but I used both of them successfully). This will significantly save resources and connections to the database, for example, prepared statements and caches from the database side will be reused more. On a muscle, this is not very noticeable (although how to say it), but on a saibase or oracle, even very much! commons.apache.org/proper/commons-dbcp and www.mchange.com/projects/c3p0
There are a lot of usage examples in the documentation, even ready-made classes and for threads it seems...

E
elbars, 2017-02-15
@elbars

In general, I figured out the problem. Really. fail2ban is not as simple as it seems, very capricious. when it comes to non-standard logs. He does not write about what he does not like, he simply does not work out. I rewrote the filter from scratch, the logs were parsed, matched, but apparently he did not like the result. In general, something like that.

Y
Yuri Chudnovsky, 2017-02-14
@Frankenstine

Either I already forgot the regexps, or you are matching lines starting with '[]', i.e. your regexp is wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question