简体   繁体   English

多个线程如何通过使用Java中的同一连接来写入数据库?

[英]How can multiple Threads write to the database by using the same connection in java?

I have a program that creates multiple threads, and i need that any one of them will write to the DB. 我有一个创建多个线程的程序,我需要其中任何一个都可以写入数据库。 the problem is that if i use the same connection the data is incorrect due to the access of multiple threads to the same Variables (like statment.setString()). 问题是,如果我使用相同的连接,则由于多个线程访问相同的变量(如statment.setString()),导致数据不正确。 if i use different connection it Takes all the benefit from the threads. 如果我使用不同的连接,它将从线程中受益。 In summary: i need that all threads will access a class or another thread that will hold 1 connection and will hold a batch of queries and once in a while will perfome execute batch. 总结:我需要所有线程都将访问一个类或另一个拥有1个连接并拥有一批查询的线程,并且不时地执行一批批处理。 thank you! 谢谢!

I see no point in doing this but if you want to do it anyway, then I suggest you synchronize the access to the DB through this connection. 我认为这样做没有任何意义,但是如果您仍然想这样做,那么建议您通过此连接同步对数据库的访问。 Add some common LOCK object and do this: 添加一些常见的LOCK对象,并执行以下操作:

synchronized(LOCK){
    // use connection by current thread including
    // sensitive operations which
    // need this synchronization
}

But then note that even though you're using multiple threads they will wait on each other, ie their access to the DB through this connection will be serialized (not simultaneous). 但是请注意,即使您正在使用多个线程,它们也会彼此等待,即它们通过此连接对DB的访问将被序列化(而不是同时进行)。

The situation you are describing (many threads accessing to a DB) is exactly what is current in web application. 您所描述的情况(许多线程正在访问数据库)正是Web应用程序中当前的情况。 And the recommended practice is to set a pool of connections to mitigate between the contention that would arise with one single connection and the resource consumption with one connection per thread. 推荐的做法是设置一个连接池,以减轻单个连接的争用和每个线程一个连接的资源消耗。 Apache DBCP is one example of such connection pool. Apache DBCP是这种连接池的一个示例。

如果您无法初始化多个连接,请在您的连接类中添加一个名为bound的布尔标志,单个线程不能使用连接“ if(bound)” ...使用该连接的任何线程都应在清除该标志时完成...在每个线程中添加while循环以检查该标志,直到它变为false,然后将其设置为true并开始使用它...相当容易...您还可以为所有接受网络的com创建第4个专用同步线程从队列中的其他线程执行任务并同步执行它们,因此不会混乱...无论如何,有大约一百万种方法,选择更适合您的应用程序的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java - 通过在java中使用相同的连接,多个线程读取到数据库中的同一个表? - multiple Threads read to the same table in database by using the same connection in java? Java中的for循环如何让多个线程同时运行? - How to make multiple Threads run at the same time using a for loop in Java? 用JAVA写多线程 - Write Multiple Threads in JAVA 如何在Java(Selenium)设置中使用Apache POI多个线程(并行测试用例)同时访问相同的excel文件? - How can multiple threads (parallel testcases) access same excel file at same time using Apache POI in Java (Selenium) setup? 如何将同一数据库连接用于多种方法(JAVA) - How to use the same database connection for multiple methods(JAVA) 在不锁定的情况下从多个线程写入同一文件,Java - write from multiple threads on same file without locking, Java 如何在Java中将多个线程和类写入同一个日志文件? - How do I make multiple threads and classes write into the same log file in Java? 多个线程可以同时将数据写入文件吗? - Can multiple threads write data into a file at the same time? 如何获得与使用Java的JPA相同的数据库连接? - How to get the same database connection as JPA used using Java? 使用多线程读取和一个线程写入内存数据库 - Using multiple threads to read and one thread to write to an in-memory database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM