Database transaction processing in Java

Source: Internet
Author: User

/*java uses transactions, first requiring the database to support transactions, such as the use of MySQL transaction functionality,
MySQL is required to have a table type of innodb,*/
/*innodb, one of MySQL's database engines
Compared with the traditional ISAM and MyISAM, InnoDB's biggest feature is the support
The acid-compatible transaction (Transaction) feature, similar to PostgreSQL. */
There are three types of Java transactions: JDBC Transaction, JTA (Java Transaction API) transaction, container transaction.

1. The JDBC transaction--------java.sql.Connection (JDBC Interface) provides two transaction modes, automatic commit and manual commit,

/* Auto-commit: When the record is updated, the system automatically commits, cannot maintain the consistency of the transaction, and cannot guarantee the data integrity.
Manual commit: It takes the transaction to be done by you, and in the event of an exception, the transaction can be rolled back, keeping the transaction consistent.
The following is a discussion of application differences in practice:
1. In JDBC, if the method Setautocommit (Boolean autocommit) of the connection class is assigned a value of false, then it can be manually committed (invoking the commit () method of the connection class);
2, if True then is automatic submission. Automatic submission you don't have to worry about the last issue to be submitted, he will automatically complete, and each statement is treated as a transaction;
3, manual submission, before you show the submission of all statements are considered a transaction, its advantage is that when one of the statements in this transaction fails, the transaction will be rolled back, that is, will not write to the database, which is conducive to maintaining the consistency of the database.
For example, when you insert related data both in the primary table and from the table, if you fail from the table (or the primary table), and the primary table (or the table) succeeds, the data is not written to the primary table (or from the table).

*/

Java.sqk.Connection provides the following methods for controlling transactions
public void Setautocommit (Boolean)

public boolean getautocommit ()

public void commit ()

public void rollback ()


/* In the JDBC API, the default is to commit transactions automatically, that is, each updated SQL statement on the database represents a transaction and the operation succeeds
, the system automatically calls commit () to commit, or rollback () is called to revoke the transaction
When using JDBC transaction demarcation, multiple SQL statements can be combined into one transaction, and one disadvantage of the JDBC transaction is that the scope of the transaction is limited to a single database
Connection, a JDBC transaction cannot span multiple databases */
public int Delete (int sID) {
Dbc=new databaseconnection ();
Connection con=dbc.getconnection ();
try{
Con.setautocommit (FALSE); BES set as false to avoid auto commit
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 ();//commit the transaction;
Con.setautocommit (TRUE); Restore the default submission method for a JDBC transaction
Dbc.close ();
return 1;

}catch (Exception exe) {
Con.rollback (); Rollback the JDBC transaction
Exe.printstacktrace ();
Dbc.close ();
return-1;
}

}
JTA (Java Transaction API) transaction
/*jta is a high-level, unrelated, protocol-agnostic API that enables applications and application servers to access transactions using JTA.
JTA allows applications to perform distributed transactions-access and update data on two or more network computer resources
Can be distributed across multiple databases. The JTA support of the JDBC driver greatly enhances the data access capability.
If you plan to define transactions with JTA, then you need to have an implementation javax.sql.XADataSource, javax.sql.XAConnection
and the JDBC driver for the Javax.sql.XAResource interface. A driver that implements these interfaces will be able to participate in the JTA transaction
JDBC Connection.
You need to set up Xadatasource with the application server's administration tools, which you can learn from the application server and the JDBC driver documentation.
Relevant guidance.
The Java EE application uses Jndi to query the data source, and once the application finds the data object, it calls Javax.sql.DataSource.getConnection.commit ()
or Java.sql.Connection.rollback ().
Instead, applications should use Usertransaction.begin (), Usertransaction.commit (), and Sertransaction.rollback ().


*/
/*//3, container transactions

Container transactions are mainly provided by the Java EE Application Server, and the container transactions are mostly based on JTA, which is a jndi-based, fairly complex API implementation.
Relative coding implements JTA transaction management, and we can accomplish the same function through the container transaction management mechanism (CMT) provided by the EJB container, which is implemented by the Java EE
Provided by the application server. This allows us to simply specify which method to join the transaction, and once specified, the container will be responsible for the transaction management task. This is our civil
Solution, because in this way we can exclude the transaction code from the logic code, while all the difficulties are given to the Java EE container to solve.
Another benefit of using EJB CMT is that programmers do not have to care about the encoding of the JTA API, but theoretically we have to use ejb.*/

Database transaction processing in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.