Java多线程数据库访问 [英] Java multiple threads database access

查看:662
本文介绍了Java多线程数据库访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于多线程Java应用程序确保所有线程同步访问db的最佳解决方案是什么?例如,每个线程代表单独的事务,然后首先检查db的值,然后根据答案必须插入或更新数据库中的某些字段(请注意在check,insert和commit应用程序之间进行其他处理).但是问题是,另一个线程可能在同一张表上做同样的事情.

更具体的例子.线程T1开始事务,然后检查表ENTITY_TABLE中是否存在代码为"111"的条目.如果找到更新日期,如果找不到则插入新条目,然后提交事务.现在想象一下线程T2所做的完全相同.现在有几个问题:

  1. T1和T2检查db,什么也没找到,并且都插入相同的条目.
  2. T1检查数据库,找到具有旧日期的条目,但是在提交T2时,它已将条目更新为较新的日期.
  3. 如果我们使用高速缓存并同步对高速缓存的访问,则会遇到问题:T1获取锁检查db,如果找不到高速缓存,则添加高速缓存,释放锁,提交. T2做同样的事情,在缓存中找到要提交的条目.但是T1事务失败并被回滚.现在T2的状态不好,因为它应该插入ENTITY_TABLE但不知道.
  4. 更多?

我正在创建具有同步功能的简单自定义缓存并解决问题3.但是也许还有一些更简单的解决方案?

解决方案

应该主要通过配置所需的乐观或悲观).

没有事务隔离,您将很难尝试仅在Java域中确保事务完整性.尤其要考虑到,即使当前仅通过Java应用程序访问数据库,将来也可能会发生变化.

现在要选择哪种隔离级别,从您的描述中看来,您需要最高的隔离级别 serializable .但是,在实践中 由于广泛的锁定,成为真正的性能猪.因此,您可能需要重新评估您的要求,以便在特定情况下找到最佳的隔离和性能平衡.

What could be the best solution for multithreaded Java application to ensure that all threads access db synchronously? For example, each thread represents separate transaction, and first checks db for value and then depending on answer has to insert or update some fields in database (note between check, insert and commit application is doing other processing). But the problem is that another thread might be doing just the same thing on same table.

More specific example. Thread T1 starts transaction, then checks table ENTITY_TABLE for entry with code '111'. If found updates its date, if not found inserts new entry, then commits transaction. Now imagine thread T2 does exactly same thing. Now there are few problems:

  1. T1 and T2 checks db and find nothing and both insert same entry.
  2. T1 checks db, find entry with old date, but on commit T2 already has updated entry to more recent date.
  3. If we use cache and synchronize access to cache we have a problem: T1 acquires lock checks db and cache if not found add to cache, release lock, commit. T2 does the same, finds entry in cache going to commit. But T1 transaction fails and is roll backed. Now T2 is in bad shape, because it should insert to ENTITY_TABLE but doesn't know that.
  4. more?

I'm working on creating simple custom cache with syncronization and solving problem 3. But maybe there is some more simple solution?

解决方案

This should be dealt with primarily within the DB, by configuring the desired transaction isolation level. Then on top of this, you need to select your locking strategy (optimistic or pessimistic).

Without transaction isolation, you will have a hard time trying to ensure transaction integrity solely in the Java domain. Especially taking into consideration that even if the DB is currently accessed only from your Java app, this may change in the future.

Now as to which isolation level to choose, from your description it might seem that you need the highest isolation level, serializable. However, in practice this tends to be a real performance hog due to extensive locking. So you may want to reevaluate your requirements to find the best balance of isolation and performance for your specific situation.

这篇关于Java多线程数据库访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆