首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

小弟我们爱分享-事务下上文

2012-12-31 
我们爱分享----事务上下文要想进行事务管理,有一个概念必须要清楚,即:必须保证开始事务,和关闭事务是同一

我们爱分享----事务上下文

     要想进行事务管理,有一个概念必须要清楚,即:必须保证开始事务,和关闭事务是同一个数据库连接。 事务上下文的主要实现,依赖于ThreadLocal,关于该类的作用,不多讲。不清楚的同学可以自己Google查一下。 以面以基于c3po数据源管理为例。


一、封装一个简单的连接池
     


           /**
 * @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
 * @功能描述:连接池接口
 * @Since Mar 11, 2011
 * 
 */
public interface PooledConnection {

/**
 * 
 * @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
 * @功能描述:初始化
 * @Since Mar 11, 2011
 */
public  void init();

/**
 * 
 * @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
 * @功能描述:关闭连接池
 * @Since Mar 11, 2011
 */
public void destory();

/**
 * 
 * @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
 * @功能描述:获取连接
 * @Since Mar 11, 2011
 * @return
 * @throws SQLException
 */
public Connection getConnection() throws SQLException;

public void releaseConnection(Connection conn) throws SQLException;
}
 
      




/**
 * @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
 * @功能描述:基于C3PO连接池的实现类
 * @Since Mar 11, 2011
 *
 */
public class RYTPooledConnection implements PooledConnection {

/**
 * c3po的数据源
 */
private  static ComboPooledDataSource ds;

/**
 * 关闭数据源
 */
public final void destory() {
ds.close();
}

/**
 * 获取数据库连接
 */
public final  Connection getConnection() throws SQLException {
return ds.getConnection();
}



/**
 * 初始化
 */
public final  void init() {
if(null == ds){
ds = new ComboPooledDataSource();
}
}

public void releaseConnection(Connection conn) throws SQLException {
if(null != conn){
conn.close();
}
}
}





以上代码,就是对c3po就行了一个简单的封装,之所以提供接口,目的相信大家都懂,为了方便扩展,以后若是不用C3PO怎么办? 很多人会问,你这里没有提供连接池的大小,初始化连接信息之类,那么我告诉你。你只需要在src目录下,提供
c3p0.properties文件,c3po数据源会自动初始化这些信息。



c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/testdb
c3p0.user=root
c3p0.password=123456
c3p0.initialPoolSize=10
c3p0.minPoolSize=10
c3p0.maxPoolSize=100


    这样,我们第一部分的工作就做完了。


二、我们来对事务对象进行一个抽象。定义一个事务对象:

     

public class Transaction {

/**database connection**/
private Connection connection;

/***trasaction begon count***/
private Integer transCount;

/*****commited cout**/
private Integer commitCount;



/***Deep**/
private Integer transDeep;

/**
 * 
 * @Function:judge is all transaction commited
 * @Since Mar 30, 2011
 * @return
 */
public boolean isFullExecute(){
return  commitCount + 1 == transCount;
}




public Integer getTransDeep() {
return transDeep;
}

public void setTransDeep(Integer transDeep) {
this.transDeep = transDeep;
}

public Connection getConnection() {
return connection;
}

public Integer getTransCount() {
return transCount;
}

public Integer getCommitCount() {
return commitCount;
}

public void setConnection(Connection connection) {
this.connection = connection;
}

public void setTransCount(Integer transCount) {
this.transCount = transCount;
}

public void setCommitCount(Integer commitCount) {
this.commitCount = commitCount;
}


}

     



    关于transCount和commitCount作一个简单的说明,这两个属性是我们来模仿一个简单的嵌套事务的。 这个嵌套事务不像spring的嵌套事务那么复杂。 仅仅是逻辑上的嵌套而已,我们定义一个开启事务的次数,以及一个提交的次数。这样,我们可以用两者判断事务是不是完全提交了。

 


   
[解决办法]
才20分
党内同志友情帮顶

热点排行