1,编程式事务管理(jdbc的事务是绑定在connection上的)
Connection conn = null
try
{
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","password")
conn.setAutoCommit(false) //取消自动提交
PreparedStatement ps = conn.prepareCall("update something")
ResultSet rs = ps.executeQuery()
conn.commit() //手动提交
}
catch (Exception e)
{
conn.rollback()
e.printStackTrace()
}
finally
{
conn.close()
}
2,声明式事务
先在工程的application.xml配置文件中添加如下代码,开启事务
<!-- 声明式事务控制配置 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="datasource" ref="bassDataSource"></property>
</bean>
然后在你需要开启事务的接口前面添加注解
@Transactional(rollbackFor = IOException.class)
public void add(String name) throws IOException
{
System.out.println("可以再类里和方法里面添加事务注解0~0")
throw new IOException()
}
直接调用接口方法就好
分布式事务处理(mysql貌似在5.X之后才支持) 的话,
1.可以直接使用spring+atomikos框架进行管理
参考:http://blog.chinaunix.net/uid-21162795-id-3424973.html
就不贴测试代码了,自己看着配置吧
2,使用JTA(Java Transaction API)进行分布式事务管理(测试代码如下)
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.SQLException
import javax.naming.InitialContext
import javax.sql.DataSource
import javax.transaction.SystemException
import javax.transaction.UserTransaction
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource
//分布式事务处理
public class transferAccount
{
@SuppressWarnings("null")
public void testTransferAccount()
{
UserTransaction userts = null
Connection connA = null
PreparedStatement psA = null
InitialContext context = null
Connection connB = null
PreparedStatement psB = null
try
{
//获得事务管理对象
userts = (UserTransaction) context.lookup("java:comp/UserTransaction")
//获取两个数据库
connA = getDataSourceA().getConnection()
connB = getDataSourceB().getConnection()
//开启事务
userts.begin()
//sql语句
psA = connA.prepareStatement("我加1")
psB = connB.prepareStatement("我减1")
//执行sql
psA.executeUpdate()
psB.executeUpdate()
//事务提交
userts.commit()
} catch (Exception e)
{
try
{
userts.rollback()
} catch (IllegalStateException | SecurityException
| SystemException e1)
{
e1.printStackTrace()
}
e.printStackTrace()
}
finally
{
try
{
psA.close()
psB.close()
connA.close()
connB.close()
} catch (SQLException e)
{
e.printStackTrace()
}
}
}
public DataSource getDataSourceA()
{
MysqlDataSource dataSource = new MysqlDataSource()
dataSource.setDatabaseName("mysql")
dataSource.setServerName("server")
dataSource.setPortNumber(1433)
dataSource.setUser("test")
dataSource.setPassword("test")
return dataSource
}
public DataSource getDataSourceB()
{
MysqlDataSource dataSource = new MysqlDataSource()
dataSource.setDatabaseName("mysql")
dataSource.setServerName("server")
dataSource.setPortNumber(1435)
dataSource.setUser("test1")
dataSource.setPassword("test1")
return dataSource
}
}
什么是事务? \x0d\x0a\x0d\x0a事务是逻辑上的一组 *** 作,组成这组 *** 作的各个单元,要不全都成功要不全都失败,这个特性就是事务 \x0d\x0a\x0d\x0a注意:mysql数据支持事务,但是要求必须是innoDB存储引擎 \x0d\x0a\x0d\x0a解决这个问题: \x0d\x0a\x0d\x0amysql的事务解决这个问题,因为mysql的事务特性,要求这组 *** 作,要不全都成功,要不全都失败,这样就避免了某个 *** 作成功某个 *** 作失败。利于数据的安全 \x0d\x0a\x0d\x0a如何使用: \x0d\x0a\x0d\x0a(1)在执行sql语句之前,我们要开启事务 start transaction\x0d\x0a\x0d\x0a(2)正常执行我们的sql语句 \x0d\x0a\x0d\x0a(3)当sql语句执行完毕,存在两种情况: \x0d\x0a\x0d\x0a1,全都成功,我们要将sql语句对数据库造成的影响提交到数据库中,committ \x0d\x0a\x0d\x0a2,某些sql语句失败,我们执行rollback(回滚),将对数据库 *** 作赶紧撤销 \x0d\x0a\x0d\x0a(注意:mysql数据支持事务,但是要求必须是innoDB存储引擎) \x0d\x0amysql>create table bank(name varchar(20),money decimal(5,1))engine=innodb defau \x0d\x0alt charset=utf8\x0d\x0a\x0d\x0amysql>inset into bank values('shaotuo',1000),('laohu',5000)\x0d\x0a\x0d\x0amysql>select*from bank\x0d\x0a+---------+--------+ \x0d\x0a| name | money | \x0d\x0a+---------+--------+ \x0d\x0a| shaotuo | 1000.0 | \x0d\x0a| laohu | 5000.0 | \x0d\x0a+---------+--------+ \x0d\x0a\x0d\x0a------没有成功“回滚”执行rollback \x0d\x0amysql>start transaction//开启事务 \x0d\x0aQuery OK, 0 rows affected (0.00 sec) \x0d\x0a\x0d\x0amysql>update bank set money=money+500 where name='shaotuo'\x0d\x0aQuery OK, 1 row affected (0.00 sec) \x0d\x0aRows matched: 1 Changed: 1 Warnings: 0 \x0d\x0a\x0d\x0amysql>update bank set moey=money-500 where name='laohu'\x0d\x0aERROR 1054 (42S22): Unknown column 'moey' in 'field list' \x0d\x0amysql>rollback//只要有一个不成功,执行rollback *** 作 \x0d\x0aQuery OK, 0 rows affected (0.01 sec) \x0d\x0a\x0d\x0amysql>select*from bank\x0d\x0a+---------+--------+ \x0d\x0a| name | money | \x0d\x0a+---------+--------+ \x0d\x0a| shaotuo | 1000.0 | \x0d\x0a| laohu | 5000.0 | \x0d\x0a+---------+--------+ \x0d\x0a------成功之后 进行commit *** 作 \x0d\x0amysql>start transaction//开启事务 \x0d\x0aQuery OK, 0 rows affected (0.00 sec) \x0d\x0a\x0d\x0amysql>update bank set money=money+500 where name='shaotuo'\x0d\x0aQuery OK, 1 row affected (0.01 sec) \x0d\x0aRows matched: 1 Changed: 1 Warnings: 0 \x0d\x0a\x0d\x0amysql>update bank set money=money-500 where name='laohu'\x0d\x0aQuery OK, 1 row affected (0.00 sec) \x0d\x0aRows matched: 1 Changed: 1 Warnings: 0 \x0d\x0a\x0d\x0amysql>commit//两个都成功后执行commit(只要不执行commit,sql语句不会对真实的数据库造成影响) \x0d\x0aQuery OK, 0 rows affected (0.05 sec) \x0d\x0a\x0d\x0amysql>select*from bank\x0d\x0a+---------+--------+ \x0d\x0a| name | money | \x0d\x0a+---------+--------+ \x0d\x0a| shaotuo | 1500.0 | \x0d\x0a| laohu | 4500.0 | \x0d\x0a+---------+--------+欢迎分享,转载请注明来源:内存溢出
评论列表(0条)