public void begin(boolean autocommit) throws DBTransException {
try { if(!this.isdatasource)
{
log("DB:before get connection");
conn = DriverManager.getConnection(strUrl, strUser, strPass);
log("DB:after get connection");
}
else
{
String jndiname = bundle.getString("jndiname");
log("DB:before datasource get connection, jndiname:" + jndiname);
Context ctx = new InitialContext();
//Context envCtx = (Context) ctx.lookup("java:comp/env");
datasource = (DataSource) ctx.lookup(jndiname);
conn = datasource.getConnection();
log("DB:after datasource get connection");
}
} catch (Exception se) {
log("DB:SQL Exception");
se.printStackTrace();
throw new DBTransException(DBTransException.ERR_GET_CONN, bundle);
} this.autocommit = autocommit;
try {
if (autocommit) {
log("DB:auto commit on");
conn.setAutoCommit(true);
} else {
log("DB:auto commit off");
conn.setAutoCommit(false);
}
} catch (SQLException e) {
throw new DBTransException(DBTransException.ERR_SET_AUTOCOMMIT, bundle);
}
}
看我上面的代码,每次开始事务都要查找ctx.lookup(jndiname);寻找连接池,这样是不是不对啊?是不是只需要查找一次,就把DataSource保存起来,每次datasource.getConnection();就行了?
该怎样做了?
解决方案 »
- Spring整合hibernate自动创建表的问题
- JAVA POI还有JXLS报表系统怎么能够横向填充EXCEL表格
- 如何在所有客户端上进行显示和方法调用
- struts2 如何生存xml文件?
- 请问Struts中<logic:present name="XXX"> 如何使用?
- Spring能够动态监听配置文件的修改么
- 严重: Catalina.stop: java.net.ConnectException: Connection refused 请问这个问题怎么解决?
- 用eclipse开发struts,请问怎么配置eclipse?
- 老师布置的数据结构作业用java做。就是关于英语论文的文本分析及词频统计,然后用三元组表示出来,论文做行,单词做列
- 求助大神
- hibernate配置文件找不到映射文件user.hbm.xml
- 求一个用于mvc设计模式的分页的javabean
还有一个关闭的closeConnection()
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;/**
* Database transaction class
* @author xi sha sha
*
*/
public class DBTransaction { private DataSource datasource = null;
private Connection conn = null;
private ResourceBundle bundle = null;
private String strDriver = "";
private String strUrl = "";
private String strUser = "";
private String strPass = "";
private boolean debug = false;
private boolean autocommit = false;
private boolean isdatasource = false;
/**
* construct db connaction
* @throws DBTransException
*/
public DBTransaction() throws DBTransException{
try {
bundle = ResourceBundle.getBundle("db");
this.strDriver = bundle.getString("dbDriver");
this.strUrl = bundle.getString("dbUrl");
this.strUser = bundle.getString("dbUser");
this.strPass = bundle.getString("dbPass");
if(bundle.getString("debug").equals("yes"))
this.debug = true;
if(bundle.getString("isDataSource").equals("yes"))
this.isdatasource = true;
} catch (Exception e) {
e.printStackTrace();
}
try {
log("DB:loading Driver");
Class.forName(this.strDriver);
log("DB:loading Driver done");
} catch (ClassNotFoundException e) {
throw new DBTransException(DBTransException.ERR_DRIVER, bundle);
}
}
/**
* begin transaction,auto commit is true
* @throws DBTransException
*/
public void begin() throws DBTransException {
begin(true);
}
/**
* begin transaction
* @param autocommit true auto commit ,false no auto
* @throws DBTransException
*/
public void begin(boolean autocommit) throws DBTransException {
try { if(!this.isdatasource)
{
log("DB:before get connection");
conn = DriverManager.getConnection(strUrl, strUser, strPass);
log("DB:after get connection");
}
else
{
String jndiname = bundle.getString("jndiname");
log("DB:before datasource get connection, jndiname:" + jndiname);
Context ctx = new InitialContext();
//Context envCtx = (Context) ctx.lookup("java:comp/env");
datasource = (DataSource) ctx.lookup(jndiname);
conn = datasource.getConnection();
log("DB:after datasource get connection");
}
} catch (Exception se) {
log("DB:SQL Exception");
se.printStackTrace();
throw new DBTransException(DBTransException.ERR_GET_CONN, bundle);
} this.autocommit = autocommit;
try {
if (autocommit) {
log("DB:auto commit on");
conn.setAutoCommit(true);
} else {
log("DB:auto commit off");
conn.setAutoCommit(false);
}
} catch (SQLException e) {
throw new DBTransException(DBTransException.ERR_SET_AUTOCOMMIT, bundle);
}
}
/**
* commit transaction
* @throws DBTransException
*/
public void commit() throws DBTransException{
if(!this.autocommit)
{
log("DB:in commit");
try {
conn.commit();
log("DB:after commit");
} catch (SQLException e) {
rollback();
throw new DBTransException(DBTransException.ERR_COMMIT_TRAN, bundle);
}
finally{
close();
}
}
close();
}
/**
* rollback data
* @throws DBTransException
*/
public void rollback() throws DBTransException{
try {
conn.rollback();
close();
log("DB:after rollback");
} catch (SQLException e) {
throw new DBTransException(DBTransException.ERR_ROLLBACK, bundle);
}
}
/**
* close connection
* @throws DBTransException
*/
public void close() throws DBTransException{
try {
if(null != conn && !conn.isClosed())
{
conn.close();
log("DB:after conn close");
}
} catch (SQLException e) {
throw new DBTransException(DBTransException.ERR_CLOSE_CONN, bundle);
}
}
/**
* get Connection object
* @return
*/
public Connection getConnection() {
log("DB:get conn is " + (conn != null?"OK":"NULL"));
return conn;
}
/**
* print log
* @param mess
*/
private void log(String mess){
if(this.debug)
System.out.println(mess);
}
}看我上面的代码,每次开始事务都要查找ctx.lookup(jndiname);寻找连接池,这样是不是不对啊?是不是只需要查找一次,就把DataSource保存起来,每次datasource.getConnection();就行了?
该怎样做了?
每次都要DBTransaction tran = new DBTransaction(); 这样对不对?
但是DataSource也每次重新查找好像不合理。
可以放入静态代码块中static{ ... }
工具类最好是全部静态方法或者是单例类
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}可以参照