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