2018年12月29日 星期六

PostgreSQL 在 READ_COMMITTED 模式的特性

在閱讀官方文件時,雖然官方文件是寫得挺詳細的,但有種很像是把程式邏輯攤出來解釋的感覺,有時候需要稍微思考一下才能把前後的描述合併起來理解。

non-repeatable read 和 phantom read

在 PostgreSQL 的文件中,描述到 SQL 標準對於錯誤的描述,分別有以下的敘述在描述 non-repeatable read 和 phantom read:

nonrepeatable read
A transaction re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read).

phantom read
A transaction re-executes a query returning a set of rows that satisfy a search condition and finds that the set of rows satisfying the condition has changed due to another recently-committed transaction.

本來對於兩者的差別感覺不是非常明顯,因此查了一下。看了 wikipedia 的描述以後,發現其實也蠻好懂的:

A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads.

A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.

也就是說,當在交易中連續讀取兩次的時候,non-repeatable read 指的是讀取到的資料列的內容不一樣,而 phantom read 指的是讀取到的資料列不一樣,例如第一次讀到 #1、#2、#3,第二次讀到 #1、#4,每個資料列的內容都一樣,但符合條件的資料列們不同。(這段描述總覺得好難用中文寫 XD)

參考資料
  1. What is the difference between Non-Repeatable Read and Phantom Read?
  2. Wikipedia: Isolation (database systems)

(暫存) Java 物件的記憶體消耗量

參考資料
  1. Memory consumption of popular Java data types part 1, part 2
  2. On the memory usage of maps in Java

2018年12月12日 星期三

在 Spring Boot 中使用 Jersey

Spring Boot 作為 Spring Framework 的入門套件,底下可以抽換不同的 RESTful 架構,預設雖然會使用 Spring MVC,但其實需要的話也可以抽換成像是 Jersey 或者其他的架構。在這裡要紀錄的就是使用 Jersey 作為 RESTful 架構的方法。

2018年12月7日 星期五

在單元測試時避免 CommandLineRunner 被執行

最近第一次嘗試寫基於 Spring Boot 的應用程式,使用了 CommandLineRunner 來作為應用程式的入口。寫的過程因為想要方便後面做單元測試,因此盡可能地把商業邏輯都寫在 CommandLineRunner 裡頭。不過結果最後在跑單元測試時,卻發現 SpringBootTest 啟動的程序會先執行 CommandLineRunner….囧rz。