Home Database Mysql Tutorial 并发环境下mysql插入检查方案_MySQL

并发环境下mysql插入检查方案_MySQL

May 27, 2016 pm 01:45 PM
mysql concurrent insert

业务背景:
基本业务场景是这样的,请求数据(车辆vin信息)进入到接口中,需要先判断其在数据库中的状态,如果库中不存在该vin,或者该vin状态位为“1(已完成)”,则执行一些检查操作后,将数据插入到数据库中,此时新增vin状态为0,调用人工处理接口,十分钟后返回结果,将状态置为1。如果其状态位为“0(正在处理)”则驳回操作,返回提示信息。
在单线程环境下,这样的业务没有问题,然而当并发访问接口时,会出现同时进入两条vin相同的请求AB,正常情况应该插入一条A,驳回一条B。然而并发环境下,B执行检查状态时A还没有插入,因此AB都进入到了数据库中,数据就错误了。

解决方案一:
首先想到的是使用sql处理,对数据库对应字段加唯一索引,保证一致性。如果插入重复的数据,则catch该异常,做出提示。

ALTER tableName ADD UNIQUE [indexName] ON (tableColumns(length))
Copy after login

但是由于业务限制,vin在库中是可以重复的,多条重复数据查询最新,所以不能再vin上添加唯一索引。

解决方案二:
使用mysql事务操作,将检查是否存在和插入作为一个事务进行处理,当检查失败的时候,不进行插入。从网上搜索了一下,大致思路如下:

public static void StartTransaction(Connection con, String[] sqls) throws Exception { 
    try { 
      // 事务开始 
      con.setAutoCommit(false);  // 设置连接不自动提交,即用该连接进行的操作都不更新到数据库 
      sm = con.createStatement(); // 创建Statement对象 

      //依次执行传入的SQL语句 
      for (int i = 0; i < sqls.length; i++) { 
        sm.execute(sqls[i]);// 执行添加事物的语句 
      } 
      con.commit();  // 提交给数据库处理 
      // 事务结束 

    //捕获执行SQL语句组中的异常   
    } catch (SQLException e) { 
      try { 
        System.out.println("事务执行失败,进行回滚!\n"); 
        con.rollback(); // 若前面某条语句出现异常时,进行回滚,取消前面执行的所有操作 
      } catch (SQLException e1) { 
        e1.printStackTrace(); 
      } 
    } finally { 
      sm.close(); 
    } 
  }

Copy after login

但是这样实际上还是没有解决并发的问题,这样只是把两个操作变成了一个原子的sql操作,可以用于同时插入两条数据一致性的情况,但并不适合需求。

既然sql层面没有解决问题,就考虑从java的并发编程方向解决。
解决方案三:
java解决并发问题,首先想到的是使用内置锁或者可重入锁,基本语法如下:
·内置锁:
由于是在Servlet中进行的处理,因此使用synchronized(this)直接处理业务代码,使得并发情况下,只能有一个线程访问到该段业务代码:

synchronized(this){
  //todo1:检查vin是否存在
  //todo2:如果不存在插入vin
}
Copy after login

·可重入锁:
相当于更灵活的内置锁,在这里与内置锁基本相同

public class DashengCallBack extends HttpServlet {
  private static ReentrantLock lock= new ReentrantLock();
  protected void doGet(HttpServletRequest request, HttpServletResponse response){
    lock.lock();
    try{
      //todo1:检查vin是否存在
      //todo2:如果不存在插入vin
    }finally{
      lock.unlock();
    }
  }
}
Copy after login

经过测试,这个方案是可行的,最终没有采用的原因是因为直接使用这种方式加锁,加锁的代码太多,影响效率。

解决方案四:
设置一个查询Map,插入前存储数据,插入后删除数据,代码如下:

    ConcurrentHashMap<String, String> vinMap=new ConcurrentHashMap<String,String>();
    if(vinMap.containsKey(vin)){
      // todo1: vin 请求完毕后, 从vinInRequestMap里删掉这个vinNo
      // todo2: 返回正在查询
    }
    vinMap.put(vin, "");
    //todo3:插入vin到数据库
    vinMap.remove(vin);
  }
Copy after login

这个方案基本满足了业务需求,唯一的问题是要求接口的更新时间要与业务时间错开,否则更新接口会清空vinMap,导致库中数据混乱,出现错误。

以上就是本文的全部内容,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to set up MySQL connection pool using PHP? How to set up MySQL connection pool using PHP? Jun 04, 2024 pm 03:28 PM

Setting up a MySQL connection pool using PHP can improve performance and scalability. The steps include: 1. Install the MySQLi extension; 2. Create a connection pool class; 3. Set the connection pool configuration; 4. Create a connection pool instance; 5. Obtain and release connections. With connection pooling, applications can avoid creating a new database connection for each request, thereby improving performance.

How to delete data from MySQL table using PHP? How to delete data from MySQL table using PHP? Jun 05, 2024 pm 12:40 PM

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

See all articles