I’ve been coding for years but some time ago I asked myself this question. I never saw this solved in my university. I’m very curious about this.
So, what really happens talking about the code, the cpu? I’m very curious about the code.
For example, if you are running a desktop program and you are inserting data in your data base, what happens in the scenarios tha I mentioned above?
MyDBConnection cn = new MyDBConnection();
cn.start();
cn.beginTransaction();
cn.insertData("INSERT INTO T_TABLE VAVLUES(1,2,'WHITE LIES')");
cn.insertData("INSERT INTO T_TABLE VAVLUES(1,3,'BLACK LIES')");
-------------------------------------------------------------------
Enery power off, application hang for whatever reason.
-------------------------------------------------------------------
cn.insertData("INSERT INTO T_TABLE VAVLUES(1,4,'VOID LIES')");
cn.commit();
cn.close();
What happens after that? Nothing?
Data gets corrupted(According to a guy I know)? There is rollback or RAM gets free or hanged?
The OS tries to restore the process?
Anyone knows some procedures to endure these accidents?
Power sometimes fails, even if you are a major bank running dual power supplies per server and taking all of the other precautions that one can take (e.g. UPS, backup generator).
For that reason, databases are designed to survive sudden failure when you use a database transaction. When the database is powered on after a sudden power failure, it will use a journal / transaction log to ensure that the database is in a consistent state.