Home Database Mysql Tutorial 代码配置都没问题,为什么事务不回滚(了解Mysql数据库引擎)

代码配置都没问题,为什么事务不回滚(了解Mysql数据库引擎)

Jun 07, 2016 pm 03:57 PM
mysql Why learn affairs code number Configuration

前几天开发中遇到了一个问题,在进行调试的过程中发现配置在service层的事务不起作用,抛出异常后数据库的更新还是成功了,事务并没有回滚,然后就开始各种检查,查看spring的事务是否配置正确,进入更新方法时是否开启了事务,经过检查之后发现一切都正常,

前几天开发中遇到了一个问题,在进行调试的过程中发现配置在service层的事务不起作用,抛出异常后数据库的更新还是成功了,事务并没有回滚,然后就开始各种检查,查看spring的事务是否配置正确,进入更新方法时是否开启了事务,经过检查之后发现一切都正常,那为什么事务不回滚呢?问题出在了一个很难会去考虑的地方(数据库里创建的表不支持事务)。

Mysql数据库默认的创建表的引擎是:MYISAM,使用这种引擎的表效率高,但是不支持事务,所以我们在建表时最好是根据需求手动去指定我们需要的引擎,下面是一个能够支持事务的建表sql,可以参考一下:

CREATE TABLE   test (
	id varchar(32)  PRIMARY KEY, 
	name varchar(32), 
	create_time datetime, 
	create_user varchar(32) ,
	address varchar(20) NOT NULL ,
	remark varchar(100)
) ENGINE = InnoDB DEFAULT CHARSET=utf8
Copy after login
备注:ENGINE = InnoDB这个配置就是选择何种引擎,InnoDB这种引擎支持事务。

下面是Mysql数据库常见的几种引擎的说明,可以了解一下,下次再创建表我们就可以选择合适引擎了。

ISAM:索引顺序访问法(Index Sequential Access Mode)

ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数。因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源。ISAM的两个主要不足之处在于,它不支持事务处理,也不能够容错,如果你的硬盘崩溃了,那么数据文件就无法恢复了。如果你正在把ISAM用在关键任务应用程序里,那就必须经常备份你所有的实时数据,通过其复制特性,MYSQL能够支持这样的备份应用程序。
MyISAM:Mysql的默认存储引擎

MyISAM是MySQL的ISAM扩展格式和缺省的数据库引擎。除了提供ISAM里所没有的索引和字段管理的大量功能,MyISAM还使用一种表格锁定的机制,来优化多个并发的读写操作,其代价是你需要经常运行OPTIMIZE TABLE命令,来恢复被更新机制所浪费的空间。MyISAM还有一些有用的扩展,例如用来修复数据库文件的MyISAMCHK工具和用来恢复浪费空间的 MyISAMPACK工具。MYISAM强调了快速读取操作,这可能就是为什么MySQL受到了WEB开发如此青睐的主要原因:在WEB开发中你所进行的大量数据操作都是读取操作。所以,大多数虚拟主机提供商和INTERNET平台提供商只允许使用MYISAM格式。MyISAM格式的一个重要缺陷就是不能在表损坏后恢复数据。

HEAP:

HEAP允许只驻留在内存里的临时表格。驻留在内存里让HEAP要比ISAM和MYISAM都快,但是它所管理的数据是不稳定的,而且如果在关机之前没有进行保存,那么所有的数据都会丢失。在数据行被删除的时候,HEAP也不会浪费大量的空间。HEAP表格在你需要使用SELECT表达式来选择和操控数据的时候非常有用。要记住,在用完表格之后就删除表格。

InnoDB:

InnoDB数据库引擎都是造就MySQL灵活性的技术的直接产品,这项技术就是MYSQL++ API。在使用MYSQL的时候,你所面对的每一个挑战几乎都源于ISAM和MyISAM数据库引擎不支持事务处理(transaction process)也不支持外来键。尽管要比ISAM和 MyISAM引擎慢很多,但是InnoDB包括了对事务处理和外来键的支持,这两点都是前两个引擎所没有的。如果你的设计需要这些特性中的一者或者两者,那你就要被迫使用INNODB和BERKLEY引擎中的一个了,我们常用的是InnoDB。

InnoDB给MySQL提供了具有提交、回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎。InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读,这些特色增加了多用户部署和性能。没有在InnoDB中扩大锁定的需要,因为在InnoDB中行级锁定适合非常小的空间。InnoDB也支持FOREIGN KEY强制。在SQL查询中,你可以自由地将InnoDB类型的表与其它MySQL的表的类型混合起来,甚至在同一个查询中也可以混合。这是mysql官方的描述。

如果还想了解更多那就多去网上查查吧...

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 solve the problem of missing dependencies when installing MySQL How to solve the problem of missing dependencies when installing MySQL Apr 08, 2025 pm 12:00 PM

MySQL installation failure is usually caused by the lack of dependencies. Solution: 1. Use system package manager (such as Linux apt, yum or dnf, Windows VisualC Redistributable) to install the missing dependency libraries, such as sudoaptinstalllibmysqlclient-dev; 2. Carefully check the error information and solve complex dependencies one by one; 3. Ensure that the package manager source is configured correctly and can access the network; 4. For Windows, download and install the necessary runtime libraries. Developing the habit of reading official documents and making good use of search engines can effectively solve problems.

Laravel's geospatial: Optimization of interactive maps and large amounts of data Laravel's geospatial: Optimization of interactive maps and large amounts of data Apr 08, 2025 pm 12:24 PM

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

Solutions to the service that cannot be started after MySQL installation Solutions to the service that cannot be started after MySQL installation Apr 08, 2025 am 11:18 AM

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

Reduce storage of MySQL RDS databases with zero downtime using AWS Blue/Green deployment Reduce storage of MySQL RDS databases with zero downtime using AWS Blue/Green deployment Apr 08, 2025 pm 12:42 PM

DevOps engineers often face the challenges of RDS database optimization and update, especially in high load situations, where traditional methods are prone to risk of downtime. This article introduces AWS blue/green deployment strategy to achieve zero downtime update of RDS database. Say goodbye to the nightmare of database update downtime! This article will explain the blue/green deployment strategy in detail and provide operational steps in the AWS environment to help you update the RDS database instance without affecting service availability. Preparation First, coordinate the development team and select the period with the lowest workload traffic to update. Good DevOps practice advice to notify the team in advance. This example demonstrates how to reduce database instance storage space with zero downtime using blue/green deployment. You can use the AWSRDS console

MySQL installation error solution MySQL installation error solution Apr 08, 2025 am 10:48 AM

Common reasons and solutions for MySQL installation failure: 1. Incorrect username or password, or the MySQL service is not started, you need to check the username and password and start the service; 2. Port conflicts, you need to change the MySQL listening port or close the program that occupies port 3306; 3. The dependency library is missing, you need to use the system package manager to install the necessary dependency library; 4. Insufficient permissions, you need to use sudo or administrator rights to run the installer; 5. Incorrect configuration file, you need to check the my.cnf configuration file to ensure the configuration is correct. Only by working steadily and carefully checking can MySQL be installed smoothly.

See all articles