/* 获取connection 对象* / import oracle.jdbc.driver.OracleDriver; /** public static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new OracleDriver()); return conn; } 利用ThreadLocal保存connection对象 import java.sql.Connection; /** public static Connection currentConnection() { Connection conn = (Connection) currentConn.get(); public static void closeConnection() { } public static void openTransaction() { } public static void commit() { } public static void roolback() { } } /** void createData() throws Exception { pstmt.execute(); pstmt.close(); } } import java.sql.Connection; /** DaoB() { public static final String sql = "update t_user_bnas set id_1dispseq='1111' where id_globalsession='11200409220000001279'"; void createData() throws Exception { pstmt.execute(); pstmt.close(); } /** public static void main(String args[]) { DaoA daoA = new DaoA(); DaoB daoB = new DaoB(); void testMethod() { try { daoA.createData(); ConnectionManage.roolback(); } finally { } }
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DBUtil {
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@10.100.143.161:1521:gb02", "gib", "gib");
} catch (SQLException e) {
e.printStackTrace();
}
}
package conn;
import java.sql.SQLException;
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ConnectionManage {
private static ThreadLocal currentConn = new ThreadLocal();
if (conn == null) {
conn = DBUtil.getConnection();
currentConn.set(conn);
openTransaction();
}
return conn;
}
try {
Connection conn = (Connection) currentConn.get();
currentConn.set(null);
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection conn = currentConnection();
conn.setAutoCommit(false);
conn.setTransactionIsolation(2);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection conn = currentConnection();
if (conn != null)
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection conn = currentConnection();
if (conn != null)
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
被简化的dao实现
package conn;
import java.sql.PreparedStatement;
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DaoA {
public static final String sql = "insert into t_user_bnas (id_globalsession,id_dispseq) values('11200409220000001279','1100')";
PreparedStatement pstmt = ConnectionManage.currentConnection().prepareStatement(sql);
package conn;
import java.sql.PreparedStatement;
* @author sfluo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DaoB {
Connection conn;
this.conn = ConnectionManage.currentConnection();
}
PreparedStatement pstmt = conn.prepareStatement(sql);
}
测试例子
package conn;
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Test {
Test test = new Test();
test.testMethod();
}
daoB.createData();
} catch (Exception e) {
ConnectionManage.closeConnection();
}
是不是很爽啊,:)