跳至主要内容

Mysql Learning Time And Locking

timestamp vs datetime range

the TIMESTAMP type’s range is ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC.
the DATETIME type’s range is ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’.

why I can set timestamp a invalid default value

That is because of server SQL Mode - NO_ZERO_DATE.
From the reference: NO_ZERO_DATE - In strict mode, don’t allow ‘0000-00-00’ as a valid date. You can still insert zero dates with the IGNORE option. When not in strict mode, the date is accepted but a warning is generated.

how to init timestamp and datatime

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Mysql lock

Alter table and lock

alter table without locking table? create a new table and copy content to it, finally rename table for mysql online operation is not good enough to handle all DML operations.

mysql cluster DML and lock: As far as I can understand, cluster which have master-slave node, is more suitable for above tricks/toolkit.

Lock table for test transaction

When I want to test the transaction of spring, I have to make some exceptional situation to test roll back. One of way I can come up with is to lock a table to make sql query fail. Here is the detail:

Session A:

MariaDB [test]> lock table student write;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> select * from student;
+-----+----------+--------+-------+------------+----------+
| sid | name     | gender | birth | department | address  |
+-----+----------+--------+-------+------------+----------+
|   1 | xiaoming |      0 |  1998 | software   | jiangsu  |
|   2 | huahua   |      0 |  1996 | software   | jiangsu  |
|   3 | xiaocai  |      0 |  1995 | software   | jiangsu  |
|   4 | xiaofang |      1 |  1993 | history    | hunan    |
|   5 | hechao   |      1 |  1994 | math       | hunan    |
|   6 | chenshi  |      0 |  1996 | history    | jiangsu  |
|   7 | zhaoxiao |      1 |  1992 | math       | zhejiang |
|   8 | xiaojing |      1 |  1991 | software   | zhejiang |
+-----+----------+--------+-------+------------+----------+
8 rows in set (0.00 sec)

Another session B fails to read after session A lock the table:

MariaDB [test]> select * from student;
^CCtrl-C -- query killed. Continuing normally.
ERROR 1317 (70100): Query execution was interrupted
MariaDB [test]> select * from student;

when session A unlock table with following command

unlock tables;

another session get the query at once( with the locked time count in):

MariaDB [test]> select * from student;
+-----+----------+--------+-------+------------+----------+
| sid | name     | gender | birth | department | address  |
+-----+----------+--------+-------+------------+----------+
|   1 | xiaoming |      0 |  1998 | software   | jiangsu  |
|   2 | huahua   |      0 |  1996 | software   | jiangsu  |
|   3 | xiaocai  |      0 |  1995 | software   | jiangsu  |
|   4 | xiaofang |      1 |  1993 | history    | hunan    |
|   5 | hechao   |      1 |  1994 | math       | hunan    |
|   6 | chenshi  |      0 |  1996 | history    | jiangsu  |
|   7 | zhaoxiao |      1 |  1992 | math       | zhejiang |
|   8 | xiaojing |      1 |  1991 | software   | zhejiang |
+-----+----------+--------+-------+------------+----------+
8 rows in set (3.15 sec) <---- here

More

What should be noticed is that lock doesn’t mean transaction. The transaction provide ACID : a set of properties to make sure data integrity, while locking can only meet the atomicity requirement.

  • Atomicity
    Atomicity requires that each transaction be “all or nothing”: if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors, and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible (“atomic”), and an aborted transaction does not happen.

And an example from wikipedia show isolation failure that locking can’t provide:

  • Isolation
    The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed serially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict - as opposed to relaxed - serializability), the effects of an incomplete transaction might not even be visible to another transaction.

Consider two transactions. T1 transfers 10 from A to B. T2 transfers 10 from B to A. Combined, there are four actions:
T1 subtracts 10 from A.
T1 adds 10 to B.
T2 subtracts 10 from B.
T2 adds 10 to A.
If these operations are performed in order, isolation is maintained, although T2 must wait. Consider what happens if T1 fails half-way through. The database eliminates T1’s effects, and T2 sees only valid data.

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi...

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation ...

Learn Spring Expression Language

When reading the source code of some Spring based projects, we can see some code like following: @Value( "${env}" ) private int value ; and like following: @Autowired public void configure (MovieFinder movieFinder, @ Value ("#{ systemProperties[ 'user.region' ] } ") String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } In this way, we can inject values from different sources very conveniently, and this is the features of Spring EL. What is Spring EL? How to use this handy feature to assist our developments? Today, we are going to learn some basics of Spring EL. Features The full name of Spring EL is Spring Expression Language, which exists in form of Java string and evaluated by Spring. It supports many syntax, from simple property access to complex safe navigation – method invocation when object is not null. And the following is the feature list from Spring EL document : ...