Implementation of database transaction processing in Java
Zhang Yanthenchun
Chengdu University of Electronic Science and Technology Computer College
Abstract This article describes the method of transaction processing in Java, which tells how to use JavaBean, EJB components to implement JDBC transactions supported by J2EE application servers, respectively, JTA (Java Transaction API) transactions.
Keywords JavaBean, EJB, database, transaction, JTA
1 Javabean
JavaBean is a component-independent component written in Java language. It is a software component model describing Java, a bit similar to the concept of COM components of Microsoft. In the Java model, the JavaBean can be expanded to the Java program, which can quickly generate new applications through JavaBean combination. JavaBean can realize the reuse of the code, and there is a significant significance for the easy maintenanceability of the program. Non-visualized JavaBean, is often used in JSP programs, and database operations, etc. can be well implemented, and the separation of business logic and front program can be achieved. JavaBean exhibits increasing vitality in the application of the server.
2 EJB
EJB technology defines a set of reusable components: Enterprise JavaBeans. You can use these components, build your distributed application like wood. When you write the code, these components are combined into a specific file. Each file has one or more Enterprise Beans, plus some configuration parameters. Finally, these Enterprise Beans are configured to a platform with EJB containers. Customers are able to locate a beans through these beans's Home interface, and generate an instance of this beans. In this way, customers can call Beans Application and Remote Interface. EJB technology simplifies the development, configuration and execution of enterprise application systems written in Java language. There are three types of Enterprise Beans: Session Beans, Entity Beans, and Message-Driven Beans.
3 transaction processing:
Information is an important asset of any enterprise institution. Any enterprise department contains information inflow, flows out, and any enterprise sector controls certain information. At the same time, the information must be disseminated to the needs of the needs in the appropriate timing. Moreover, information requires a safety constraint, typically, in accordance with the type of information and the content of the information. In order to ensure the safety and effectiveness of data, the Database Management System (DBMS) must provide a unified data protection.
The transaction is one of the core concepts in the modern database theory. If a set of processing steps or all or one step is not executed, we call this group processing step for a transaction. When all steps are performed as an operation, we call this transaction. Since some or more step failures fail, there is no step to be submitted, and the transaction must roll back (return to the initial system state). The transaction must obey the ACID principles set by ISO / IEC. Acid is an abbreviation for atomicity, consistency, isolation and durability. Atomicity of the transaction indicates that any failure in the transaction execution will cause any modifications that have been made. Consistency indicates that all data imposed by this transaction should be restored to the status of transaction execution when transaction failed. Isolation indicates that the data is modified in the transaction process, which is invisible to other transactions before transaction commit. Persistence indicates that the data that has been submitted is in the event of failure of the transaction, the status should be correct.
In the following, we list an example of transaction processing using the SQL Server database. The main table is a rules and regulation information table (bylaw), the main field has record numbers, title, authors, written dates, etc. The two subtries are an attachment table (Bylaw_AFFIX) and a text information table (bylaw_content). The surface structure is shown in Figure 1. The record number of the BYLAW table is corresponding to the record number of the BYLAW_AFFIX table. The record number of the BYLAW_CONTENT table corresponds, each time the operation of the rule system information is the joint operation of these three tables. For example, to delete a record in the rule system, if you don't use a transaction, you may have the case: After successful deletion in the first table, the database suddenly occurs, and the second, the operations in the three tables are not completed. In this way, the deletion operation is not completed, and even data in the database. To avoid this, you should use transactions, it's the role of: either three tables to operate success, or fail. In other words, it is to maintain the consistency of data. Therefore, in order to ensure the integrity and consistency of data operations, it is necessary to fully take into account the issues of transaction processing in programming. Figure 1 Exemplary table structure
4 Transaction processing in Java:
In general, the J2EE application server supports JDBC transactions, Java Transaction API transactions, and container management transactions. In general, it is best not to use the above three transaction types simultaneously in the program, such as nested JDBC transactions in JTA transactions. In the second aspect, the transaction should be completed in a short period of time, do not use the transaction in different methods. Below we list two transaction methods.
4.1 JDBC mode in JavaBean is transactional
How to combine multiple SQL statements into a transaction in JDBC? In JDBC, when you open a connection object Connection, the default is Auto-Commit mode, each SQL statement is used as a transaction, that is, each time you execute a statement, you will automatically get a transaction confirmation. In order to combine multiple SQL statements into one transaction, you should block the auto-commit mode. After the auto-commit mode is broken, if a commit () method is not called, the SQL statement does not get a transaction confirmation. All SQLs after the last commit () method call will be 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 submission of JDBC transactions
Dbc.executeUpdate ("delete from bylaw where id =" sID);
Dbc.executeUpdate ("delete from bylaw _content where id =" SID);
Dbc.executeUpdate ("delete from bylaw _Affix where bylawid =" sID);
Con.commit (); // Submit a JDBC transaction
Con.SetAutoCommit (TRUE); // Restore the default submission method for the JDBC transaction
dbc.close ();
Return 1;
}
Catch (Exception EXC) {
Con. rollback (); // rollback JDBC transaction
Exc.printStackTrace ();
dbc.close ();
Return -1;
}
}
4.2 JTA transactions in SessionBean
JTA is a J2EE solution for transaction services. Essentially, it is a part of the J2EE model that describes the transaction interface (such as the UserTransaction interface, the developer directly using the interface or using the interface to secure business logic to reliably run) J2EE model. The three main interfaces with JTA are the useertransaction interface, the 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 (), which only appear on a specific interface for implementation Allow a certain degree of access control. For example, userTransaction can perform transaction division and basic transaction operations, while TransactionManager can perform context management.
The application can call the userTransaction.begin () method to start a transaction, which is associated with the current thread running in it. The underlying transaction manager actually handles the association between threads and transactions. Usertransaction.commit () method Terminate transactions associated with current threads. The useertransaction.rollback () method will abandon 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 (); // Get JTA transactions
Try {
Transaction.begin (); // Start JTA transaction
Dbc.executeUpdate ("delete from bylaw where id =" sID);
Dbc.executeUpdate ("delete from bylaw _content where id =" SID);
Dbc.executeUpdate ("delete from bylaw _Affix where bylawid =" sID);
Transaction.commit (); // Submit JTA Transactions
dbc.close ();
Return 1;
}
Catch (Exception EXC) {
Try {
Transaction.rollback (); // JTA transaction rollback
}
Catch (Exception EX) {
// JTA transaction rollback error handling
EX.PrintStackTrace ();
}
Exc.printStackTrace ();
dbc.close ();
Return -1;
}
}
references:
1. Paul J.Perrone, et al. The "J2EE Build Enterprise System - Experts and Solutions", Tsinghua University Press, June 2001;
2. Wang Shan, Sa Shiqi, etc. "Database System Introduction (Third Edition)", Higher Education Press, January 2000;
3. Bruce Eckel, "Thinking In Java-The Third Edition", Machinery Industry Press, October 2002.