The running, entity, and message-driven type EJB components run, depending on the data resource stored in the database, or store data information to the database. For enterprise information systems related to EJB components in the J2EE application system, the accuracy, reliability, consistency, and timeliness of the data must be maintained. For example, in the J2EE application system in the financial sector, if the system is transferring between two accounts according to customer requirements. If the application system increases the balance of the loan account, the EJB server suddenly crashes, and the application system does not reduce the balance of the loan account. In this case, there is no normal end for one transfer operation, and the data record corresponding to the EJB component is a fatal (FATAL) error, which will cause loss to the bank. To this end, the EJB specification defines the corresponding component transaction (Transaction) management, used to maintain consistency and stability of components corresponding data resources.
Two types of component transactions are defined in the EJB2.0 specification: Container-Managed transactions and component management (Bean-management) transactions. Component programming staff and assemblers can choose component transaction management implementations for component types and transaction management policies.
Container management method for business
If you set the transaction management feature of the component to the container management method, you need to set the EJB container of the component to set the transaction range of the method in the component and control the process of the transaction. Component programming does not need to write a transaction in the implementation code of the component. Manage code to greatly simplify the development process of component programs.
Transaction attribute
For sessions, entities, and messaging components, it can set the transaction feature of the component as a type of container management. When the component method in the transaction range is called, the EJB container immediately starts a transaction until the method runs.
The reader may think that the transaction type of setting components is the container management type, and each method in the client calls the EJB component notifies the EJB container to start a transaction process, and the actual situation is not the case. During the EJB component assembly of the container management transaction type, you can specify the transaction management of the EJB container to the components by setting the transaction attribute of the component. EJB container supports the following types of transaction properties:
◇ Required: When the client application within a transaction calls the component business method, the component business method is executed within the original client transaction;
◇ Requirednew: When the client application within the transaction calls the component business method, the EJB container launches a new transaction process, and the component business method is executed within the new transaction process;
◇ Mandatory: If the client application that calls the EJB component business method is not within the transaction range, the EJB container throws the transactionRequiredException and enforces the client to start the transaction process;
◇ NotSupported: The business method of the EJB component does not need to run during the transaction. If the client application of the EJB component method is in the transaction, the original transaction process hangs when the component business method is called until the component method is running;
◇ Supports: Component methods must be within the transaction. If the client called the component business method is not in the transaction, the EJB container starts a new transaction process;
◇ Never: Component Method does not need to run during the transaction. If the client application of the component business method is in the transaction range, the EJB container throws the RemoteException exception.
The component transaction attributes can be set to the EJB component during the component development process or only the above properties apply to a method in the component. It should be noted that if transaction properties are set for component methods, the methods in different types of EJB components are different from the requirements of the transaction attribute type. The session type EJB component allows the business method for the component to set transaction properties, not allowing the Create method of the creation component to include within the transaction; the entity type EJB component can set transaction attributes for component business methods, Create, REMOVE, and FIND methods; The drive assembly allows only the INMessage method of the component to the Required and NotSupported properties. Container management transaction rollback
The method of using the container management transaction type can use two policies to notify the EJB container roll back the running results of the components method (rollback). A strategy is that the method in the transaction range throws the system, and the other is to invite the SetrollbackOnly method in the component method to inform the EJB container.
When the EJB container returns the transaction rollback, the SQL statement within the transaction is usually revoked all changes made to the component corresponding to the data resource. For entity type EJB components, EJB containers revoked changes to instance variables in components; for session type EJB components, EJB containers typically call methods in the sessionsynchronization interface to reset all instance variables in the component.
The Sessionsynchronization interface includes three methods: AfterBegin, BeforeCompletion, and AfterCompletion, the instance variables in the component class are synchronized with the corresponding database records in different phases during the transaction.
Before the client calls the business method of the EJB component, the EJB container calls the instance variable EJB container in the instance variable EJB container in notification component that has started a new transaction, and the value of the corresponding field in the database can be assigned to the instance variable in the component. .
The component business method is running, and the EJB container calls the BeforeCompletion method before the transaction is submitted. In this method, the instance variables in the assembly can be synchronized with their corresponding database record fields.
If the EJB container calls the AFTERCOMPLETION method, the entire transaction process is over. If the parameter value of the logical type in this method is if true, the method in which the method within the transaction is successful, the transaction process confirms the submission, and the EJB container rolls back the results of the process of the entire transaction, EJB container uses the corresponding field in the database record The instance variable of the content update component.
Component management method for transaction
The reader may think that the use of container transaction management can fully meet the EJB components. In fact, when the component method within the transaction is run, the EJB container deployed by the component must complete the transaction process of the component, which makes the EJB component for the transaction control of their own methods. In addition, if the component corresponding to the database server will cause the component transaction attribute to reset. In order to enhance the transaction control capability of the component and the portability between the different database servers, the EJB specification defines another type of component transaction management mode-transaction component management, so that component programming can be able to implement code in implementation of components Write a transaction control code, set transaction control, enhance the flexibility of component transaction control. It should be noted that the component management method of the transaction is only available for session type and message-driven type EJB components, and the physical type EJB component can only use the container management type of transactions.
Component programming can use two types of transactions component management: JDBC transaction management methods and JTA transaction management methods.
JDBC transaction management method
In order to understand the JDBC transaction management method, first please read the following code:
Public void dosomething () {
Try {
Con.SetAutocommit (False);
DemoBusinessMethod ();
C.Commit ();
} catch (exception exp) {
Try {
Con. rollback (); throw new EJBEXCEPTION ("Transaction Failed:" Exp.getMessage ());
} catch (SQLEXCEPTION EXP1) {
Throw New EJBEXCEPTION ("Rollback Failed:" Exp1.getMessage ());
}
}
}
Before the component business method in the above code, first call the setOutocommit method of the Connection object instance CON and specify the parameter FALSE in the method, and notify the database server not automatically submit the execution result of the SQL statement. When the method in the component is successfully executed, call all the COMMIT method in the CON object instance submit all database operation results. In the process of execution, if any type of exception occurs, all methods are rolled back to all methods in the transaction.
JDBC transaction management is implemented through the transaction manager of the database management system. In this manner, after the Connect method in the java.sql.connection object is bound, the relevant database manager will start a transaction process. When the component method runs, you can call the commission to submit a run result or call the Rollback method to roll the ROLLBACK method according to the operation of the component business method.
JTA transaction management
Java Transaction API (Java Transaction API, JTA) is used to provide transaction components that are unrelated to the database manager for EJB components. JTA is a high-level application programming interface for Java Transaction Service (JTS) such that the corresponding method of the component program can control the transaction range and transaction management methods in JTA. Please read the following code:
Public void doanotherthing () {
UserTransaction Ut = context.getusertransaction ();
Try {
Ut.begin ();
DemoBusinessMethod ();
Ut.commit ();
} catch (exception exp) {
Try {
Ut.rollback ();
} catch (systemException exp1) {
Throw New EJBEXCEPTION ("Rollback Failed:" Exp1.getMessage ());
}
Throw new EJBEXCEPTION ("Transaction Failed:" Exp.getMessage ());
}
}
In the code above, the reader first sees the user transaction interface object Usertransaction. The Begin, Commit, and Rollback methods for determining the components transaction range and control component transaction are defined in this interface. The Begin method in which the component method is first called before the application is determined to determine the transaction process of the component, and if there is no exception in the execution of the component business method, call the commit method to submit the operation result of the method within the transaction, otherwise call the rollback method Methods The results roll back and throw the EJBEXCEPTION type exception.
Summary of transaction management
For different types of EJB components, different component transaction management methods can be selected in component development and assembly. The following table summarizes the transaction management method applicable to different types of EJB components:
Table 1 Summary table 1 component type and optional transaction management
Component Type Container Transaction Management Mode Component Transaction Management JDBC JTA Session Components YYY Entity Component YNN Message Drive Components YYY
As you can see from the above table: Any other transaction management methods are suitable for all types of EJB components in addition to the entity components cannot be used in JDBC and JTA transaction management. At this point, the EJB series lecture is considered a paragraph. In this series of lectures, we mainly introduce the concept of EJB architecture and EJB session components, entity components, message driver components, and other components and simple applications. I hope our EJB series lectures can help your practical application. The following is the full content of the EJB series lecture: