Posts

How to set Isolation Level for each transaction in Golang

psqlInfo := fmt . Sprintf ( "host=%s port=%d user=%s " + "password=%s dbname=%s sslmode=disable" , host , port , user , password , dbname ) db , err := sqlx . Open ( "postgres" , psqlInfo ) // create a TX tx , err := db . BeginTxx ( context , & sql . TxOptions { Isolation : sql . LevelDefault }) rows , err := tx . Query ( `select amount from person where id=$1` , 1 ) .... Belows is some types of level in Golang support // IsolationLevel is the transaction isolation level used in TxOptions. type IsolationLevel int // Various isolation levels that drivers may support in BeginTx. // If a driver does not support a given isolation level an error may be returned. // // See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels. const ( LevelDefault IsolationLevel = iota LevelReadUncommitted LevelReadCommitted LevelWriteCommitted LevelRepeatableRead LevelSnapshot LevelSerializable LevelLi...

Transaction Isolation

Image
Ref: https://www.postgresql.org/docs/9.5/transaction-iso.html The SQL standard defines four levels of transaction isolation. The most strict is Serializable, which is defined by the standard in a paragraph which says that any concurrent execution of a set of Serializable transactions is guaranteed to produce the same effect as running them one at a time in some order. The other three levels are defined in terms of phenomena, resulting from interaction between concurrent transactions, which must not occur at each level. The standard notes that due to the definition of Serializable, none of these phenomena are possible at that level. (This is hardly surprising -- if the effect of the transactions must be consistent with having been run one at a time, how could you see any phenomena caused by interactions?) The phenomena which are prohibited at various levels are: dirty read A transaction reads data written by a concurrent uncommitted transaction. nonrepeatable read A transact...

What is connection pool and why do we need it?

When your application needs to access your database several times. It is easy to open a connection for each request But it is not a performance application, to enhance the performance we need to care for POOL definition. I mean connection pool in database //Golang code psqlInfo := fmt . Sprintf ( "host=%s port=%d user=%s " + "password=%s dbname=%s sslmode=disable" , host , port , user , password , dbname ) db , err := sqlx . Open ( "postgres" , psqlInfo ) if err != nil { panic ( err ) } //set connection pool with maximum is 30 db . SetMaxOpenConns ( 30 ) //always available at least 5 connections for using db . SetMaxIdleConns ( 5 ) // db . SetConnMaxLifetime ( time . Minute )

How to avoid LOST UPDATE in database

A lost update will happen when there are many transactions which concurrently modify on one column This is one transaction that update amount for the person which has id = 1 func updatePersonWith ( db * sqlx . DB , ctx context . Context , extra int ) { context := ctx tx , err := db . BeginTx ( context , & sql . TxOptions { Isolation : sql .LevelDefault}) if err != nil { log . Infof ( "update1 Create tx fail %v" , err ) } rows , err := tx . Query ( `select amount from person where id=$1` , 1 ) if err != nil { log . Infof ( "Exec sql failed %v" , err ) } var amount int for rows . Next () { if err := rows . Err (); err != nil { log . Infof ( "Exect sql get amount1 failed %v" , err ) } err = rows . Scan (& amount ) log . Infof ( "amount= %d" , amount ) break } rows . Close () //just for test time . Sleep ( time . Second * 3 ) amount = amount...

User initialization scripts

Systemwide settings scripts In the /etc/ folder, the following files are related to the user level initialization: • /etc/profile : Few distributions will have additional folder /etc/ profile.d/ . All the scripts from the profile.d folder will be executed. • /etc/bash.bashrc User level settings – default files Scripts in the /etc/ folder will be called for all the users. Particular user-specific initialization scripts are located in the HOME folder of each user. These are as follows: • $HOME/.bash_profile : This contains user-specific bash environment default settings. This script is called during the login process. • $HOME/.bash_login : This contains the second user environment initialization script called during login process. • $HOME/.profile : If present, this script internally calls the .bashrc script file. • $HOME/.bashrc : This is an interactive shell or terminal initialization script.

Understanding run levels in ubuntu

Run level number| Description 0 Halting the system 1 Single-user mode 2 Multi-user mode 3 Multi-user with network support 4 Not used 5 Graphical user interface with multi-user and networking support 6 Rebooting the system

how to use Curl cmd

curl -X POST -H "x-api-key: xscsss" -F "file=@/path/to/OLSTXNRF_tcbs_20191226_01.TXT" https://host:2189/ftpdata/tcbs/user_input/OLSTXNRF_tcbs_20191226_01.TXT Method: POST x-api-key: header -F chỉ ra là file file: là filename @/path/to/OLSTXNRF_tcbs_20191226_01.TXT    : Đường dân tới file, bao gồm cả @ https://host:2189/ftpdata/tcbs/user_input/OLSTXNRF_tcbs_20191226_01.TXT   : là url api  ------------------------------------------------------------------------------------ pull a GET request from a url or uri curl http://www.example.com/index.html pull and save response to a file curl http://www.example.com/index.html -o out.exxxx  -------------------------------------------------------------------------------------- post data to url  curl -d '{"id":9,"name":"baeldung"}' -H 'Content-Type: application/json'    http://localhost:8082/spring-rest/foos/new pass a file containing ...