自动提交行为 JDBC 规范要求在每个数据操作语句后缺省执行 COMMIT。当前,客户端 JDBC 行为是提交(自动提交为 true),服务器端行为是不提交(自动提交为 false)。要在客户端和服务器端应用程序中均获得相同的行为,您可以使用诸如以下的语句:
con.setAutoCommit( false ); |
在此语句中,con 是当前连接对象。您也可以将自动提交设置为 true。
设置事务隔离级别 要设置事务隔离级别,应用程序必须使用以下值之一来调用 Connection.setTransactionIsolation 方法。
SQL Anywhere JDBC 4.0 驱动程序:
下例使用 JDBC 4.0 驱动程序将事务隔离级别设置为 SNAPSHOT。
try { con.setTransactionIsolation( sybase.jdbc4.sqlanywhere.IConnection.SA_TRANSACTION_SNAPSHOT ); } catch( Exception e ) { System.err.println( "Error! Could not set isolation level" ); System.err.println( e.getMessage() ); printExceptions( (SQLException)e ); } |
有关 getTransactionIsolation 和 setTransactionIsolation 的详细信息,请参见关于 java.sql.Connection 接口的文档 ( http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/)。
连接缺省值
从服务器端的 JDBC 对 getConnection( "jdbc:default:connection" )
的调用中,只有第一个调用使用缺省值创建新连接。后续调用返回当前连接的包装,所有连接属性均保持不变。如果在初始连接中将自动提交设置为 false,则同一 Java 代码中,任何后续 getConnection 调用所返回的连接中的自动提交均会设置为
false。
您可能希望确保关闭连接时会使连接属性恢复为缺省值,这样,所获得的后续连接就会采用标准的 JDBC 值。以下代码可实现这一点:
Connection con = DriverManager.getConnection("jdbc:default:connection"); boolean oldAutoCommit = con.getAutoCommit(); try { // main body of code here } finally { con.setAutoCommit( oldAutoCommit ); } |
此处的讨论不仅适用于自动提交,也适用于其它连接属性,如事务隔离级别和只读模式。
有关 getTransactionIsolation、setTransactionIsolation 和 isReadOnly 方法的详细信息,请参见关于 java.sql.Connection 接口的文档 ( http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/)。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2013, SAP 股份公司或其关联公司. - SAP Sybase SQL Anywhere 16.0 |