H2 数据库使用

怎么使用 h2 数据库。

1. 引入依赖

1
2
3
4
dependencies {
  api('p6spy:p6spy')
  api('com.h2database:h2')
}

2. 以内存的方式使用

1
2
3
4
5
# spring boot 配置
spring:
  datasource:
    driverClassName: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:h2:mem:test;DB_CLOSE_DELAY=1000

3. 以进程的方式使用

1
2
3
4
5
6
7
8
# 启动 h2 数据库
java -cp h2*.jar org.h2.tools.Server -ifNotExists

# 启动 h2 console (可选)
java -cp h2*.jar org.h2.tools.Console

# 连接配置,会自动创建文件
url: jdbc:h2:tcp://localhost/~/test

4. 参考

  1. 官方文档

Openjdk Build

相关命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
sudo apt install build-essential manpages-dev software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update && sudo apt install gcc-11 g++-11

1. sudo apt update && sudo apt upgrade gcc libfontconfig1-dev systemtap-sdt-dev libx11-dev


sudo apt-get install libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev

sudo apt-get install libcups2-dev

sudo apt-get install libasound2-dev


bash configure --build=x86_64-unknown-linux-gnu --enable-debug --with-jvm-variants=server --enable-dtrace

bash configure --enable-debug --with-jvm-variants=server 

bash configure --enable-debug --with-jvm-variants=server --with-toolchain-type=gcc --with-boot-jdk=C:/Users/ooooo/Development/Jdk/jdk17 

2. 参考

深入理解Java虚拟机(第3版) jdk build

Linux 中的 Cgroup 机制

1. 检查 cgroup 的版本

1
2
3
4
5
# check if cgroup is supported 
cat /proc/filesystems | grep cgroup

# check cgroup version 
cat /proc/mounts | grep cgroup

2. cgroup v2 操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# create new dir
cd /sys/fs/cgroup
mkdir test

# creat loop.sh for testing cpu quota
vim loop.sh
while :
do
        :
done

# lunch loop.sh, generate pid -> 2584068
nohup sh loop.sh &

# echo pid to cgroup.procs
echo 2584068 > test/cgroup.procs

# set cpu, at lease 0.1
echo 1000 10000 > test/cpu.max

# check 
top 

# recovery all
kill 2584068
rmdir test

参考:

Mysql 间隙锁

1. 准备数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# create new schema
create schema test;
use test;

# create table test
create table user
(
    id   int primary key,
    age  int
);

alter table user
    add index age_idx (age);

# insert some test data
insert into user
values (3, 10),
       (5, 20),
       (8, 30);
       

2. 间隙锁测试

1. 使用主键索引,指定行存在

1
2
3
4
5
6
7
8
9
# session 1, the row is exist for id = 3 , so it doesn't lock.
begin;

select * from user where id = 3 for update;

# session 2, execute successful.
begin;

insert into user value (1, 20);

2. 使用主键索引,指定行不存在

1
2
3
4
5
6
7
8
9
# session 1, the row isn't exist for id = 2, so it locks range (,3]
begin;

select * from user where id = 2 for update;

# session 2, execute block.
begin;

insert into user value (1, 20);

3. 使用主键索引,范围查找

1
2
3
4
5
6
7
8
9
# session 1, it locks range [1,5] 
begin;

select * from user where id >= 1 and id <= 5 for update;

# session 2, execute block
begin;

insert into user value (2, 20);

4. 使用二级索引,指定行存在

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,8]
begin;

select * from user where age = 20 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

5. 使用二级索引,指定行不存在

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,5]
begin;

select * from user where age = 15 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

6. 使用二级索引,范围查询

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,8]
begin;

select * from user where age >= 12 and age <= 28 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

7. 结论

使用主键索引,行存在时,才只会锁定这一行。 其他情况都是使用范围锁定