Databse Transaction
来源:互联网 发布:非平衡面板和缺失数据 编辑:程序博客网 时间:2024/05/01 16:54
Databse Transaction
在更新数据库时,默认情况下,更改是永久性的写入(或提交)到数据库。然而,这种默认的行为可以通过编写程序来关闭。 在自动交付关闭的情况下,如果在更新时发生问题,则对数据库的每个更改都能够取消(或者说回退到最初的值)。如果更新 成功,那么,之后就可以将这些更改永久性的提交给数据库。这就是数据库的事物管理。 典型地我们可以用try/catch/finally块来正确地应对事务管理。首先,我们记录自动提交前的状态。然后在try块中,调用 setAutoCommit(false)并执行一系列的查询或更新。如果发生故障,则在catch块中调用rollbackl如果事务成功,则在try块 的节尾调用commit.不管哪种情况都在finally块中重置自动提交的状态。
比如:
Connection connection= DriverManage.getConnection(url,username,password);
boolean autoCommit=connection.getAutoCommit();
Statement statement;
try
{
connection.setAutoCommit(false);
statement=connecion.createStatement();
statement.executeQuery(.....);
statement.execute(.....);
connecion.commit();
}
catch(SQLException e)
{
connection.rollback();
}
finally
{
statement.close(); connecion.setAutoCommit(autoCommit);
}
getConnection方法会抛出SQLException异常,这个异常要么被外围的方法抛出,要么在单独的try/catch块中进行捕获.看下面的两段代码:
package coreservlet;
import java.sql.*;
import coreservlet.beans.*;
/** An example to demonstrate submission of a block of SQL statements as s single transaction
* Specifically, four new records are inserted into the music table.Performed as a transaction
* block so that if a problem occurs, a rolback is performed and no changes are committed to
* the database.
*/
public class Transactons {
public static void main(String[] args)
{
if(args.length<5)
{
printUsage();
return;
}
String vendor= args[4];
//Change to DriverUtilites2.loadDrivers() to load vendor drivers form an XML file instead of loading
// hard-coded vendor drivers in Drivertilities.
DriverUtilities.isValidVendow(vendor);
if(!DriverUtilities.isValidVendor(vendor))
{
printUsage();
return;
}
String driver=DriverUtilities.getDriver(vendor);
String host=args[0];
String dbName=args[1];
String url=
DriverUtilities.makeURL(host,dbName,vendor);
String username=args[2];
String password=args[3];
doTransactions(driver,url,username,password);
}
private static void doTransaction(String driver,
String url,
String username,
String password)
{
String[] transaction=
{
"inset into music values"+
"(9,'Chopin','No.2 in F minor',100,17.99)",
"Insert into music values"+
"(10,'Tchaikovsky','No.1 in Bb minor',100,24,99)",
"Insert into music values"+
"(11,'Ravel','No.2 in D major',100,14.99)",
"Insert into music values"+
"(12,'Schumann','No.1 in A minor',100,14.99)"
};
TransactionBean bean=new TransactionBean();
try
{
bean.setConnection(driver,url,username,password);
bean.execute(transaction);
}
catch(SQLException sqle)
{
System.err.println("Transaction failutr"+sqle);
}
finally
{
bean.close();
}
}
private static void printUsage()
{
System.out.println("Usage:Transaction host"+
"dbName username password"+
"vendor.");
}
}
package coreservlet;
import java.io.*;
import java.sql.*;
import java.util.*;
import coreservlet.*;
/** Bean for performing JDBC taransactions. After specifying the connection, submit a block of SQL statement
* as a single transaction by calling execute. If an SQLException occurs,any prior statements are automatically
* rolled back.
*/
public class TransactionBean {
private Connection connection;
public void setConnecion(Connection connection)
{
this.connection=connection;
}
public void setConnecion(String driver,String url,String username,String password)
{
setConnection(ConnectionInfoBean.getConnection(driver,url,username,password));
}
public Connecion getConnection()
{
return(connection);
}
public void execute(List list) throws SQLException
{
execute((String[])list.toArray(new String[list.size()]));
}
public void execute(String transaction) throws SQLException
{
execute(new String[]{transaction});
}
/** Execute a block of SQL Statement as s single transaction. If an SLQException occurs, a rollback
* is attempted and the exception is thrown.
*/
public void execute(String[] transaction) throws SQLException
{
if(connecion==null)
{
throw new SQLException("No connection available.");
}
boolean autoCommit=connection.getAutoCommit();
try
{
connection.setAutoCommit(false);
Statement statement=connection.createStatement();
for(int i=0;i<transaction.length;i++)
{
statement.execute(transaction[i]);
}
statment.close();
}
catch(SQLException sqle)
{
connection.rollback();
throw sqle;
}
finally
{
connection.commit();
connection.setAutoCommit(autoCommit);
}
}
public void close()
{
if(connection==null)
{
try
{
connection.close();
}
catch(SQLException sqle)
{
System.err.print("Failed to close connecion:"+sqle);
}
finally
{
connnecion=null;
}
}
}
}
- Databse Transaction
- Backup & Restore Databse
- databse学习资源
- How to connect oracle databse
- Transaction
- Transaction
- Transaction
- transaction
- Transaction
- Transaction
- Transaction
- Transaction
- @Transaction
- transaction
- Transaction
- restore databse 和 recover database 区别
- Oracle下的Databse,Instance,Schemas
- Add Migration Update-Databse 遇到的问题
- [摘录]CBD片语-001
- 用Web Services整合.NET和J2EE
- 什么时候应该使用Web Service
- 清华生7天猎头生活的发现——IT这行,有钱人真多(转)
- 用ASP.NET开发Web服务的五则技巧
- Databse Transaction
- Debug的使用
- 移动硬盘常见问题的解决
- 调用.NET XML Web Services返回的数据集合的方法
- -- 理解VC工程
- Using XML Data Islands in Mozilla(Mozilla中使用XML数据岛)
- 给本本换硬盘,直接克隆旧盘!
- SOAP协议规范
- SOA Psummarize SOAP概述