JDO transaction introduction
Foreword
Transaction is responsible for maintaining the integrity of JDO data. It has the following features:
l Undispetent: This feature represents a transaction to operate in a transaction, or all execute all fail, it is impossible to implement one part.
l Consistency: it maintains the consistency of database data.
l Independence: Every business is independent. That is to say, it is impossible to operate by another related transaction when you read and write a persistent object in your business.
l Sull use: The life cycle of a transaction is very long, as long as the data storage service does not stop it, it can always exist.
The characteristics of the transaction described above are very important, in order to understand me to give an example:
Suppose you want to make a bank's system, you have to achieve such a feature, that is, transferred between users, this program is approximately in the case:
Public void TransferFunds (user from, user to, double amnt)
{
// Fund transfer
From.DecrementAccount (Amnt);
// fund transfer
To.incrementAccount (Amnt);
}
Ok, according to the above example, now suppose has a user A to give the user B transfer 1000 yuan. Then you can use the above way to pass 3 grid parameters, from: a, to: b, Amnt: 1000. First of all, you have turned from the user A to the 1000 yuan, this is a problem that the problem cannot be distinguished (for example, there is an abnormality or just hardware has a problem) Your program is not executed, that is, user B The account did not transfer the money. Oh, 1000 yuan is like this, why? Because your data is not synchronized, you don't use a transaction. If the two steps above the transfer operation are in the same transaction, then this problem will not occur, or the one-time transfer from here again to another account, or two accounts remain unchanged.
1. Transaction type
The transaction type has two main types: Database transaction processing (or negative business processing "pessimistic") also has active (OptionImistic) transaction. We cannot simply distinguish between the same meaning from the literal meaning, and they have the advantages and disadvantages.
Negative transaction processing will use the record lock to locate other transactions when the transaction is performed. This can effectively avoid the conflict between transactions but it also brings the consumption of database resources, and this may cause recording deadlocks. The problem that solves the deadlock needs to rely on the database itself.
Positive transaction processing is small in resource consumed by negative transactions, but relatively poor stability. Because positive transaction processing does not lock the record, multiple transactions will have a case where the same record is handled, and if the two transactions update the same record, the conflict between transaction is caused.
2. JDO transaction interface
The Transaction interface is responsible for controlling transactions in JDO, which has a range of getter, setter methods, and standard transactional division methods, and methods of judging the currently running transaction.
Public Boolean getnontransactionalread ();
Public void setnontrarsactionalRead (Boolean Read);
Public Boolean getnontransactionalwrite ();
Public void setnontransactionAlwrite (Boolean Write);
Public boolean getretainvalues ();
Public void setRetainvalues (Boolean retain); public boolean getrestoreValues ();
Public void setRestoreValues (Boolean Restore);
Public boolean getoptimistic ();
Public void setoptimistic (Boolean Optimistic);
Public synchronization getSynchronization ();
Public void setsynchronization (SYNCHRONIZATION SYNCH);
Transaction's NontransactionalRead, NontransactionalWrite, RetainValues, RestoreValues, and Optimistic correspond to the appropriate ways in PersistenceManagerFactory. The last Synchronization method allows you to access Javax.Transaction.Synchronization so you can extend this method to implement your own commit or rollback.
Public void begin ();
Public void commit ();
Public void rollback ();
The above three methods From the literal we can, begin start a transaction, and commit is submitted to the database, Rollback rollback data. If you have an exception JDO, you will automatically roll back the data.
Public boolean isactive ();
The above method returns to whether the current transaction is an active transaction.
Now let's use Transaction examples of the example of the bank transfer:
Public void TransferFunds (user from, user to, double amnt)
{
PersistenceManager PM = JDOHELPER.GETPERSISTENCEMANAGER (FROM);
Transaction trans = PM.currentTransaction ();
Trans.begin ();
Try
{
From.DecrementAccount (Amnt);
To.incrementAccount (Amnt);
TRANS.COMMIT ();
}
Catch (JDofaTalexception JFE) // This situation JDO will automatically roll back data
{
Throw JFE;
}
Catch (runtimeexception re) // Other exceptions roll back by investigation rollback
{
TRANS. ROLLBACK ();
Throw RE;
}
}