SummaryThis article describes how to process transactions in Java, and describes how to use the JavaBean and EJB components to implement JDBC transactions and JTA (Java transaction API) transactions supported by the J2EE application server.
KeywordsJavaBean, EJB, database, transaction processing, JTA
JavaBean
Javabean is a platform-independent component written in Java. It is a software component model describing Java, a bit similar to Microsoft's COM component concept. In the Java model, Java program functions can be infinitely expanded through JavaBean, and new applications can be quickly generated through the combination of JavaBean. JavaBean can reuse code, which is also of great significance for easy maintainability of the program. Non-visualized Javabean is often used in JSP programs to encapsulate transaction logic, database operations, and so on, which can well achieve the separation of business logic and foreground programs. JavaBean has shown more and more vitality in server applications.
EJB
EJB technology defines a set of reusable components: Enterprise JavaBeans. You can use these components to build your distributed applications like building blocks. After you write the code, these components are combined into a specific file. Each file has one or more enterprise beans, and some configuration parameters are added. Finally, these enterprise beans are configured on a platform with an EJB container installed. Through the home interface of these beans, the customer can locate a beans and generate an instance of the beans. In this way, the customer can call the application method and remote interface of beans. The EJB technology simplifies the development, configuration, and execution of enterprise application systems written in Java. There are three types of enterprise beans: session beans, entity beans, and message-driven beans.
Transaction Processing
Information is an important asset of any enterprise or institution. Any enterprise Department contains inbound and outbound information, and any enterprise Department controls certain information. At the same time, information must be transmitted to the desired person at the appropriate time. In addition, information also requires security constraints. Generally, access control is implemented based on the type and content of information. To ensure data security, effectiveness, and reliability, the database management system (DBMS) must provide unified data protection functions.
Transactions are one of the core concepts in modern database theory. If a group of processing steps either occur or are not executed, we call the group processing steps as a transaction. When all the steps are fully executed like an operation, we say the transaction is committed. The transaction must be rolled back (back to the initial system status) because some or more of the steps fail to be committed ). Transactions must follow the acid principles established by ISO/IEC. Acid is short for Atomicity, consistency, isolation, and durability. The atomicity of a transaction indicates that any failure during the transaction execution will invalidate any modification made by the transaction. Consistency indicates that when the transaction fails, all data affected by the transaction should be restored to the status before the transaction is executed. Isolation indicates the modification of data during transaction execution, which is invisible to other transactions before the transaction is committed. Durability indicates that the data status should be correct when the transaction fails.
The following is an example of using the SQL Server database for transaction processing. A master table is a rule and regulations information table (Bylaw). Its main fields include record numbers, titles, authors, and writing dates. The two sub-tables are the attachment table (bylaw_affix) and the text information table (bylaw_content ). The table structure is shown in Figure 1. The record number of the bylaw table corresponds to the record number of the bylaw_affix table and the record number of the bylaw_content table. Each operation on the rules and regulations information is the joint operation on the three tables. For example, if you want to delete a record in the rules and regulations, if you do not use a transaction, this may happen: after the first table is successfully deleted, the database suddenly experienced an unexpected situation, the operations in Tables 2 and 3 are not completed, so the deletion operation is not completed, and even the data in the database has been damaged. To avoid this situation, you should use a transaction. Its function is to either operate on all three tables successfully or fail. In other words, data consistency is maintained. Therefore, in order to ensure the integrity and consistency of data operations, the transaction processing issues should be fully taken into account in programming.
Figure 1 Example Table Structure |
Java Transaction Processing
Generally, the J2EE Application Server supports JDBC transactions, JTA (Java transaction API) transactions, and container management transactions. Generally, it is better not to use the above three transaction types in the program at the same time, such as nesting JDBC transactions in JTA transactions. Second, the transaction should be completed in the shortest time. Do not use the transaction in different methods. The following lists two transaction processing methods.
1. Use JDBC for transaction processing in JavaBean
In JDBC, how does one combine multiple SQL statements into one transaction? In JDBC, when a connection object is opened, the default mode is auto-commit. Each SQL statement is treated as a transaction, that is, each statement is automatically confirmed by the transaction. In order to combine multiple SQL statements into a transaction, We need to block the auto-commit mode. If the Commit () method is not called after the auto-commit mode is disabled, the SQL statement will not be confirmed by the transaction. All SQL statements after the last call of the Commit () method are confirmed when the method commit () is called.
Public int Delete (INT Sid ){
DBC = new databaseconnection ();
Connection con = DBC. getconnection ();
Try {
Con. setautocommit (false); // change the default commit method of JDBC transactions
Dbc.exe cuteupdate ("delete from Bylaw where id =" + Sid );
Dbc.exe cuteupdate ("delete from Bylaw _ content where id =" + Sid );
Dbc.exe cuteupdate ("delete from Bylaw _ affix where bylawid =" + Sid );
Con. Commit (); // submit a JDBC transaction
Con. setautocommit (true); // restore the default commit Method for JDBC transactions
DBC. Close ();
Return 1;
}
Catch (exception exc ){
Con. rollback (); // roll back JDBC transactions
Exc. printstacktrace ();
DBC. Close ();
Return-1;
}
} |
2. JTA transactions in sessionbean
JTA is a J2EE solution for transaction services. Essentially, it is part of the J2EE model that describes the transaction interface (such as the usertransaction interface, which developers directly use or use through the J2EE container to ensure that the business logic can run reliably. JTA has three main interfaces: usertransaction interface, transactionmanager interface, and transaction interface. These interfaces share common transaction operations, such as commit () and rollback (), but also include special transaction operations, such as suspend (), resume (), and enlist (), they only appear on specific interfaces to allow a certain degree of access control in the implementation. For example, usertransaction can perform Transaction Division and basic transaction operations, while transactionmanager can perform context management.
An application can call the usertransaction. Begin () method to start a transaction. This transaction is associated with the current thread in which the application is running. The association between the underlying transaction manager and the actual processing thread. The usertransaction. Commit () method terminates the transaction associated with the current thread. The usertransaction. rollback () method will discard the current transaction associated with the current thread.
Public int Delete (INT Sid ){
Databaseconnection DBC = NULL;
DBC = new databaseconnection ();
DBC. getconnection ();
Usertransaction transaction = sessioncontext. getusertransaction (); // obtain the JTA transaction
Try {
Transaction. Begin (); // start the JTA transaction
Dbc.exe cuteupdate ("delete from Bylaw where id =" + Sid );
Dbc.exe cuteupdate ("delete from Bylaw _ content where id =" + Sid );
Dbc.exe cuteupdate ("delete from Bylaw _ affix where bylawid =" + Sid );
Transaction. Commit (); // submit a JTA transaction
DBC. Close ();
Return 1;
}
Catch (exception exc ){
Try {
Transaction. rollback (); // JTA transaction rollback
}
Catch (exception ex ){
// Handle JTA transaction rollback errors
Ex. printstacktrace ();
}
Exc. printstacktrace ();
DBC. Close ();
Return-1;
}
} |