sqlite中的journal

journal 主要用途是,当事务要修改page时,先把未修改的page存入journal中,如果事务rollback时,就从journal中得到修改前的数据,覆盖已改的,达到事务的一致性。

 

journal是一个不同于数据库文件的另一个文件,它在事务开始时创建,当事务结束时就删除了

 

The format for the journal header is as follows:
- 8 bytes: Magic identifying journal format.
- 4 bytes: Number of records in journal, or -1 no-sync mode is on.
- 4 bytes: Random number used for page hash.
- 4 bytes: Initial database page count.
- 4 bytes: Sector size used by the process that wrote this journal.
- 4 bytes: Database page size.

 

 

** The journal file format is as follows:
**
**  (1)  8 byte prefix.  A copy of aJournalMagic[].
**  (2)  4 byte big-endian integer which is the number of valid page records
**       in the journal.  If this value is 0xffffffff, then compute the
**       number of page records from the journal size.
**  (3)  4 byte big-endian integer which is the initial value for the
**       sanity checksum.
**  (4)  4 byte integer which is the number of pages to truncate the
**       database to during a rollback.
**  (5)  4 byte big-endian integer which is the sector size.  The header
**       is this many bytes in size.
**  (6)  4 byte big-endian integer which is the page size.
**  (7)  zero padding out to the next sector size.
**  (8)  Zero or more pages instances, each as follows:
**        +  4 byte page number.
**        +  pPager->pageSize bytes of data.
**        +  4 byte checksum
**
** When we speak of the journal header, we mean the first 7 items above.
** Each entry in the journal is an instance of the 8th item.

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLite3的锁机制主要分为共享锁、独占锁和保留锁三种类型。这些锁类型的作用是为了保证数据在并发读写操作时的正确性和一致性。 1. 共享锁 共享锁是一种轻量级的锁,它允许多个读事务同时持有同一个资源的锁,但不允许写事务获取锁。多个共享锁之间不会互相阻塞,因此可以提高并发性能。共享锁的作用是保证读操作的一致性和可重复性。 2. 独占锁 独占锁是一种重量级的锁,它只允许一个事务独占一个资源的锁,其他事务无法同时获取锁,包括读操作。独占锁的作用是保证写操作的原子性和一致性。 3. 保留锁 保留锁是一种特殊的锁类型,它用于协调事务之间的锁定操作。当一个事务获取保留锁时,它可以保留一部分资源的锁,但不会阻止其他事务获取共享锁或独占锁。保留锁的作用是在事务之间协调锁的使用,避免死锁和长时间等待的问题。 在SQLite3,每个锁的类型都有对应的SQL语句进行申请和释放,如申请共享锁使用"SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE"语句,申请独占锁使用"BEGIN EXCLUSIVE"语句,申请保留锁使用"BEGIN IMMEDIATE"语句等。 需要注意的是,在SQLite3,锁的机制是自动管理的,当一个事务需要获取锁时,系统会自动分配和管理锁的状态,避免了手动管理锁的复杂性。同时,SQLite3也提供了一些PRAGMA命令来控制锁的行为,如PRAGMA locking_mode用于设置锁的模式,PRAGMA journal_mode用于设置日志模式等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值