论坛首页 Java企业应用论坛

分布式访问框架halo-dal设计思想

浏览 12314 次
精华帖 (2) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-05-07  
rain2005 写道
楼主的代码很清晰,很不错,主要的复杂点在sql解析这块


是的,如果你有好的解析sql算法,可以替换,只要 implements SQLAnalyzer,然后DALFactory.setSqlAnalyzer
0 请登录后投票
   发表时间:2012-05-07  
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?
0 请登录后投票
   发表时间:2012-05-07  
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?


DALDataSource.getCurrentDataSource 需要用到,因此我放到了threadlocal里面.
如果只有DALConnection用到,是可以放一个实例变量的,当初我也想过这个问题。。。。英雄所见略同啊。哈哈哈
0 请登录后投票
   发表时间:2012-05-07  
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?



其实主要还是由于ThreadLocal中有了,我就觉得没有必要在给一个实例变量了
0 请登录后投票
   发表时间:2012-05-07  
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?


等我在好好看看你的方案
0 请登录后投票
   发表时间:2012-05-07  
ak478288 写道
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?


DALDataSource.getCurrentDataSource 需要用到,因此我放到了threadlocal里面.
如果只有DALConnection用到,是可以放一个实例变量的,当初我也想过这个问题。。。。英雄所见略同啊。哈哈哈

呵呵,明白了,要实现DataSource继承了Wrapper接口,这个Wrapper接口太操蛋了。应该可以忽略它吧
0 请登录后投票
   发表时间:2012-05-07  
rain2005 写道
ak478288 写道
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?


DALDataSource.getCurrentDataSource 需要用到,因此我放到了threadlocal里面.
如果只有DALConnection用到,是可以放一个实例变量的,当初我也想过这个问题。。。。英雄所见略同啊。哈哈哈

呵呵,明白了,要实现DataSource继承了Wrapper接口,这个Wrapper接口太操蛋了。应该可以忽略它吧


DataSource还好,主要是PreparedStatement的接口,确实难写。
0 请登录后投票
   发表时间:2012-05-07  
看了下spring 的代码


/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jdbc.datasource;

import java.io.PrintWriter;
import java.sql.SQLException;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

/**
 * Abstract base class for Spring's {@link javax.sql.DataSource}
 * implementations, taking care of the padding.
 *
 * <p>'Padding' in the context of this class means default implementations
 * for certain methods from the <code>DataSource</code> interface, such as
 * {@link #getLoginTimeout()}, {@link #setLoginTimeout(int)}, and so forth.
 *
 * @author Juergen Hoeller
 * @since 07.05.2003
 * @see DriverManagerDataSource
 */
public abstract class AbstractDataSource implements DataSource {

	/** Logger available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());


	/**
	 * Returns 0, indicating the default system timeout is to be used.
	 */
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	/**
	 * Setting a login timeout is not supported.
	 */
	public void setLoginTimeout(int timeout) throws SQLException {
		throw new UnsupportedOperationException("setLoginTimeout");
	}

	/**
	 * LogWriter methods are not supported.
	 */
	public PrintWriter getLogWriter() {
		throw new UnsupportedOperationException("getLogWriter");
	}

	/**
	 * LogWriter methods are not supported.
	 */
	public void setLogWriter(PrintWriter pw) throws SQLException {
		throw new UnsupportedOperationException("setLogWriter");
	}


	//---------------------------------------------------------------------
	// Implementation of JDBC 4.0's Wrapper interface
	//---------------------------------------------------------------------

	@SuppressWarnings("unchecked")
	public <T> T  unwrap(Class<T> iface) throws SQLException {
		Assert.notNull(iface, "Interface argument must not be null");
		if (!DataSource.class.equals(iface)) {
			throw new SQLException("DataSource of type [" + getClass().getName() +
					"] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName());
		}
		return (T) this;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return DataSource.class.equals(iface);
	}

}


应该可以这样实现吧
0 请登录后投票
   发表时间:2012-05-07  
rain2005 写道
ak478288 写道
rain2005 写道
ak478288 写道
rain2005 写道
发了1个半小时把楼主的代码看了一遍,DALCurrentStatus中的STATUS_DSKEY对应的值也就算当前数据源的key应该可以直接作为DALConnection中的实例变量吧?毕竟这里没有并发访问的情况。

STATUS_DSKEY代表的就是当前需要使用的真实的DataSource。你可以看
DALPreparedStatement 中 prepare()方法这几句

DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo();
        DALFactory dalFactory = DALFactory.getInstance();
        List<Object> values = dalParameters.getValues();
        SQLInfo sqlInfo = dalFactory.getSqlAnalyzer().analyse(sql,
                values.toArray(new Object[values.size()]));
        // 如果用户没有自定义设置,那么以解析结果为dsKey
        if (dalCustomInfo == null) {
            DALCurrentStatus.setDsKey(this.parsePartitionDsKey(sqlInfo));
        }
        this.sql = dalFactory.getSqlAnalyzer()
                .outPutSQL(sqlInfo, dalCustomInfo);

这里真实的DataSource的数据是保存在ThreadLocal变量里面的,我的意思是这里好像可以直接把值保存在DALConnection中,加一个实例变量就可以了,不知道这样可行否?


DALDataSource.getCurrentDataSource 需要用到,因此我放到了threadlocal里面.
如果只有DALConnection用到,是可以放一个实例变量的,当初我也想过这个问题。。。。英雄所见略同啊。哈哈哈

呵呵,明白了,要实现DataSource继承了Wrapper接口,这个Wrapper接口太操蛋了。应该可以忽略它吧


你说的 Wrapper接口是什么?不明白
0 请登录后投票
   发表时间:2012-05-07  
就是

@SuppressWarnings("unchecked")  
    public <T> T  unwrap(Class<T> iface) throws SQLException {  
        Assert.notNull(iface, "Interface argument must not be null");  
        if (!DataSource.class.equals(iface)) {  
            throw new SQLException("DataSource of type [" + getClass().getName() +  
                    "] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName());  
        }  
        return (T) this;  
    }  
  
    public boolean isWrapperFor(Class<?> iface) throws SQLException {  
        return DataSource.class.equals(iface);  
    }  

这两个方法就是Wrapper要实现的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics