Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root", "password");
Let's say in Java, we can create a mysql connection via the code above. From the connection
object, we can create few statement
objects as below:
statement = connection.createStatement();
I would like to know, if we execute those statement
object (by calling statement.executeQuery
) in different threads, will they execute synchronously or asynchronously in Mysql database? Because what I know is, one connection in mysql will be handled as one thread, so my thinking is, all the statements that are created by that connection will schedule in its queue. Am I correct?
So, if I have a servlet
like below:
public class HelloServlet extends HttpServlet {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root", "password");
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
statement = connection.createStatement();
}
}
From the code above, if there are more than one user connect to the servlet
at the same time, will they block each other because the statement cannot execute parallel at the same time? So, they have to wait previous statement finish executing before their turn take on? Any way to avoid this kind of problem?