Tom is sending Jerry 500 dollars.
1. If there are multiple operations, each operation uses its own separate connection, there is no guarantee that the transaction
2, the specific steps:
1> start transaction before transaction begins: Cancels the default commit behavior of connection
2> if the operation of the transaction is successful, the transaction is committed
3> ROLLBACK TRANSACTION: If an exception occurs, the transaction is rolled back in the catch block;
3. Template try {
Start transaction: Cancels default commit (default Auto-commit) Con.setautocommit (false);
//...
Commit Transaction Con.commit ();
} catch (Exception e) {
//...
If there is an exception, roll back.
try {
Con.rollback ();
} catch (SQLException E1) {
E1.printstacktrace ();
}
}
@Test
public void Testtransaction () {
Connection con = null;
try {
con = jdbctools.getconnection ();
System.out.println ("Default transaction:" + Con.getautocommit ());
//Start transaction: Cancel default commit (default Auto-commit)
Con.setautocommit (false);
String sql = "Update users set balance=balance-500 where id=1";
update (con, SQL);
int i = 10/0;
sql = "Update users set balance=balance+500 where id=2";
update (con, SQL);
//Submit Transaction
Con.commit ();
} catch (Exception e) {
Rolling back a transaction if an exception occurs
try {
Con.rollback ();
} catch (SQLException E1) {
E1.printstacktrace ();
}
} finally {
Jdbctools.release (null, NULL, con);
}
}
to set the isolation level of a transaction
@Test
public void Testtransactionisolationread () {
String sql = "Select Balance from Users where id=1";
Integer balance=getforvalue (SQL);
System.out.println ("balance=" +balance);
}
/*
* Test the isolation level of a transaction the isolation level of the transaction can be set in the JDBC program through the Connection Settransactionisolation () method
*/
@Test
public void Testtransactionisolationupdate () {
Connection con = null;
try {
con = jdbctools.getconnection ();
Start transaction: Cancel default commit (default Auto-commit)
Con.setautocommit (FALSE);
String sql = "Update users set balance=balance-500 where id=1";
Update (CON,SQL);
Con.commit ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
}
}
Returns the property value of an object
@SuppressWarnings ("Unchecked")
Public <E> E Getforvalue (String sql, Object ... args) {
1. Get the result set: The result set should be one row and only one column
Connection con = null;
PreparedStatement PS = null;
ResultSet rs = null;
try {
con = jdbctools.getconnection ();
Con.settransactionisolation (connection.transaction_read_uncommitted);
Con.settransactionisolation (connection.transaction_read_committed);
PS = con.preparestatement (SQL);
for (int i = 0; i < args.length; i++) {
Ps.setobject (i + 1, args[i]);
}
rs = Ps.executequery ();
if (Rs.next ()) {
Return (E) rs.getobject (1);
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
Jdbctools.release (RS, ps, con);
}
2, obtain the result set
return null;
}
The updated Jdbctools.java tool classes are as follows:
Import Java.io.InputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
/*
* JDBC Tool class
* */
public class Jdbctools {
Commit a transaction
public static void commit (Connection con) {
if (con! = null) {
try {
Con.commit ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Rolling back a transaction
public static void rollback (Connection con) {
if (con! = null) {
try {
Con.rollback ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Start a transaction
public static void Begintx (Connection con) {
if (con! = null) {
try {
Con.setautocommit (FALSE);
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
/*
* Method of executing SQL Insert,update,delete
*/
public static void Update (String sql, Object ... args) {
Connection conn = null;
PreparedStatement PS = null;
try {
/*
* 1, get connection Connection 2, Get Statement 3, SQL statement 4, close database connection
*
*/
conn = getconnection ();
PS = conn.preparestatement (SQL);
for (int i = 0; i < args.length; i++) {
Ps.setobject (i + 1, args[i]);
}
Ps.executeupdate ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
Release (NULL, PS, conn);
}
}
public static Connection getconnection () throws Exception {
String driverclass = null;
String jdbcurl = null;
String user = null;
String password = null;
Read the Jdbc.properties file under the Classpath
InputStream in = JDBCTools.class.getClassLoader (). getResourceAsStream ("jdbc.properties");
Properties Properties = new properties ();
Properties.load (in);
Driverclass = Properties.getproperty ("Driver");
Jdbcurl = Properties.getproperty ("Jdbcurl");
user = Properties.getproperty ("user");
Password = properties.getproperty ("password");
Load Database Driver
Class.forName (Driverclass);
Get a database connection by DriverManager's getconnection () method
Connection Connection = drivermanager.getconnection (jdbcurl, user, password);
return connection;
}
public static void Release (ResultSet RS, Statement St, Connection conn) {
if (rs! = null) {
try {
Rs.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
if (st! = null) {
try {
St.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
IF (conn! = null) {
try {
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
}
< 11 processing of >JDBC_ transactions + isolation