利用ThreadLocal简化struts开发

王朝java/jsp·作者佚名  2004-11-18
窄屏简体版  字體: |||超大  

/* 获取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();

}

}

}

是不是很爽啊,:)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航