并发环境下mysql插入检查方案_MySQL
业务背景:
基本业务场景是这样的,请求数据(车辆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))
但是由于业务限制,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(); } }
但是这样实际上还是没有解决并发的问题,这样只是把两个操作变成了一个原子的sql操作,可以用于同时插入两条数据一致性的情况,但并不适合需求。
既然sql层面没有解决问题,就考虑从java的并发编程方向解决。
解决方案三:
java解决并发问题,首先想到的是使用内置锁或者可重入锁,基本语法如下:
·内置锁:
由于是在Servlet中进行的处理,因此使用synchronized(this)直接处理业务代码,使得并发情况下,只能有一个线程访问到该段业务代码:
synchronized(this){ //todo1:检查vin是否存在 //todo2:如果不存在插入vin }
·可重入锁:
相当于更灵活的内置锁,在这里与内置锁基本相同
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(); } } }
经过测试,这个方案是可行的,最终没有采用的原因是因为直接使用这种方式加锁,加锁的代码太多,影响效率。
解决方案四:
设置一个查询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); }
这个方案基本满足了业务需求,唯一的问题是要求接口的更新时间要与业务时间错开,否则更新接口会清空vinMap,导致库中数据混乱,出现错误。
以上就是本文的全部内容,希望对大家的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
