Wondering if this procedure can be accomplish without having any data error. Using PHP7 and MySQL innoDB. This is still in theory stage and if you can give good input and recommendation before the project starts would be a tremendous help.
The case will be that there will be multiple transactions and there could be multiple transactions happening at the same time even in microseconds.
The transaction
table will have a hashed column where the value of hash is the hashed of all the information of that transaction + salt + the previous hash value entry (if exist).
The process will be:
- PHP will receive transaction order (user info, amount and etc)
- PHP get the value of the previous transaction hash (if exist)
- If there is no transaction exist (first transaction), PHP will hash the entire transaction information (sha256), Hx0 and MySQL will create it's first entry with the hash value of Hx0
- If there is already a transaction before, PHP will take the previous hash value (Hx0) and hash it along with the entire current transaction info (sha256), Hx1{Hx0} and MySQL will create the new entry in the database
- This process repeats all the way between PHP and MySQL, Hxn+1{Hxn}
My concern is, if there could be a possibility of several transaction using the same hash value of the previous transaction if those transaction occurs at the same time?
For example, latest Tx hashed value is abcd. If 5 new transaction goes in at the same time in microsecond, will those 5 use that same latest Tx hashed value abcd? The result i'm trying to achieve is that it will be in order and use the latest hashed value properly.
[edited] Let me try to explain more clearly.
- First transaction will have the value of hash
hash(info + timestamp)
= "acbd" - Second transaction will have the value of
hash(info + timestamp + "abcd")
= "cdfg" - Third transaction will have the value of
hash(info + timestamp + "cdfg")
= "hijk"
My concern is that if 3 transactions enter at the same time in microseconds or nanoseconds after that third transaction, will the new 3 transactions be like as follows? (fourth transaction is still hashing with the previous hash, while the fifth transaction and sixth transaction is also hashing using the same previous hash, before fourth transaction even manage to complete and update to database) :
- Fourth transaction
hash(info + timestamp + "hijk")
= "12sd" - Fifth transaction
hash(info + timestamp + "hijk")
= "x3sd" - Sixth transaction
hash(info + timestamp + "hijk")
= "n76h"
I would expect and hope that my results to be:
- Fourth transaction
hash(info + timestamp + "hijk")
= "12sd" - Fifth transaction
hash(info + timestamp + "12sd")
= "mn34" - Sixth transaction
hash(info + timestamp + "mn34")
= "09lk"
Plan B is (what do you think?):
- Create two tables. One (table A) to receive all transaction without hashing, and another (table B) to hash each transaction in table A one by one.
- Table A will insert all entries regardless if it's in nano second simultaneously.
- Table B will just happily go through Table A and hash each entry one by one which also not affected by the simultaneous transactions and having the hash value properly in order.