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 processing, JTA 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. 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. The transaction processing information is an important asset of any enterprise institution. Any enterprise department contains information inflow, outflows, and any enterprise departments control some 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
In the case of transaction in Java, J2EE application servers support 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. 1. Use JDBC mode in JavaBean to transaction 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 submissions 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 en-en -; Submit a JDBC transaction Con.SetAutOCommit (TRUE); // Restore the default submission method of JDBC transactions dbc.close (); return 1;} catch (Exception Exc) {con?rollback (); // rollback JDBC transaction EXC.PRINTSTACKTRACE (); Dbc.close (); return -1;}} 2, JTA transaction JTA in SessionBean is the 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.