Kodojdo Guide Chapter 9

xiaoxiao2021-03-06  83

Chapter 9 affairs

The transaction is used to ensure the integrity of transaction. It focuses the operation to a working group. The operations in this group are either all successful, or all fail. There are several features of the transaction 1) Atomicity: Atomicity refers to the transaction either success - either All failed attributes. Either update data in the transaction is completed, or all fail. Not, regardless of the original state in the database. The transaction is impossible to succeed.

2) Consistency. Each transaction makes the data of the database from a coherent state to another.

3) Isolation. The transaction is isolated from each other. When you read the persistent data from the transaction, if other matters are not completed, you can't see the changes of the data caused by the transaction. Similarly, you are in a business Update operations and other concurrent transactions have not conflict. The formation of conflicts depends on your use of optimization services or use non-optimization transactions. The next section introduces these two types.

4) Durability. The role of successful matters is durable. The update of persistent data continues to the activity cycle of the data set. (The translation is not good)

These attributes are known "acidity" for the transaction. It is understood why these attributes are so important for maintaining data integrity, consider the following example: Suppose you create an application for a bank account. This program contains a method, Go to another with funds of an account. It should be like this:

Now suppose A users want to transfer 100 yuan to b users. No problem, you simply call the TransferFunds method, use the account A as a FORM parameter. As the TO parameter. 100 yuan as the total. Method execution. From a account It is reduced by 100 yuan. However, an accident occurred. Maybe it is a hardware failure. Your method is not completed. Now you have to face this situation, 100 yuan disappear. Should "thank you" your function, A The account has decreased by 100 yuan, and this 100 yuan did not go to the B account. The database is inconsistent. Now the importance of the transaction is clear. If TransferFunds are placed in a transaction together, it is impossible to have only methods. The first line of success is successful. When accidental occurs, it is either successful, or if you don't turn it off. The money will not disappear into the air, and the database will never be in a non-concealed state.

9.1 Transaction type

(Here you should be a tutorial.) There are two main types of transactions. Optimistic and pessimistic. Each type has its advantages: When pessimistic transaction is implemented, it will lock the database. Prevent other concurrent transactions from using the same data This prevents the conflict of the transaction. However, the database is consumed. In addition, locking the data will cause a deadlock. There is such a situation: two transactions are waiting for the other party to complete the lock, dead lock results Depending on the database: Usually after the specified time passes, the transaction will volunteer rollback and will throw an exception.

Optimistic transactions consume less resources. The cost is to reduce reliability. Because optimism does not lock data records, maybe two transactions will change the same lasting information at the same time, and do not check conflicts before the second transaction is submitted. At this time The second transaction will know that another concurrent transaction has modified the same record (usually through the timestamp or version system), throwing an appropriate exception. To note that the optimism still maintains data integrity, but only in severe concurrency The situation is likely to fail.

9.2 JDO transaction interface

Transaction interface management JDO transactions. This interface includes many ways to operate class Java Bean style Getter and SETTER methods, standard transaction division methods and methods for testing whether the transaction is active.

After NonTransactionalRead, NonTransactionalWrite, RetainValues, RestoreValues, and PersistenceManagerFactory Optimistic transactions in a corresponding set of properties. The final presentation attributes are not synchronous. This allows you to use a property associated with an instance javax.transaction.Synchronization transaction. Completed transaction, the transaction You will tell the synchronization instance, so you can achieve custom behavior when submitting or rollback. Reference synchronized Javadoc documentation is more details.

Begin, Commit and Rollback methods give a transactional division. As the name suggests, Begin starts a transaction, and commit is trying to submit the transaction to the database, Rollback abandoned the transaction. At this time, the database reply to the status before the transaction. If the transaction occurs during the transaction Error, JDO will automatically roll back. However, if you take the initiative to perform rollback operations, you can release some of the resources occupied.

Finally, if the transaction is active, the Isactive method returns true. (The call to the Begin method is more frequent than ", it returns False.

转载请注明原文地址:https://www.9cbs.com/read-95052.html

New Post(0)