N
N
nikesport2014-12-01 15:01:19
Java
nikesport, 2014-12-01 15:01:19

How to implement a connection for a separate thread?

The code is basically this:
class A extends Thread {
new Thread (new class B).start()
}
class B Implements Runnable{
public B () {
//defined logic
....
}
public void run(){
new db_connection() ;
}
}
class db_connection {
public db_connection() {
if (driver == null) {
try {
driver = new FabricMySQLDriver();
DriverManager.registerDriver(driver);
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, root, password);
System.out.print("Connection succeeded"+"\n");
} catch (SQLException e) {
System.out.print("Connection failed");
}
} else {
try {
connection = DriverManager.getConnection(url, root, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
}
The question is that for each thread in this case there will be a separate connection or does everyone have the same one? Logically, I understand that everyone should have their own, but it doesn’t work out that way. Please tell me what is wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Pavlov, 2014-12-01
@gurinderu

Why do you need 1 connection 1 thread?
Use pools to work with the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question