`

中间表接口案例分享001

 
阅读更多
import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.orm.hibernate3.HibernateJdbcException;

import com.vtradex.thorn.server.exception.BusinessException;

/**
 * @description 获得数据源工厂类
 */
public class DataSourceFactory  implements ApplicationContextAware{

	private static ApplicationContext ac;
	
	public static DataSource getDataSourceFactory(String dataSourceName){
		DataSource ds = null;
		try{
			ds = (DataSource)ac.getBean(dataSourceName);
		}catch(NoSuchBeanDefinitionException e) {
			throw new BusinessException("dataSource.dataBase.unActive",
new String[]{dataSourceName});
		}catch(HibernateJdbcException ex){
			throw new BusinessException("dataSource.dataBase.connect.erro",
new String[]{dataSourceName});
		}
		return ds;
	}

	public void setApplicationContext(ApplicationContext ac) throws BeansException {
		DataSourceFactory.ac = ac;
	}
}

 

/**使用案例*/
public void execute(){
	Connection connection = null;
	PreparedStatement pStatement = null;// PreparedStatement
        PreparedStatement upStatement = null;
	ResultSet resultSet = null;// ResultSet
	try{
		connection = getConnection();
		connection.setAutoCommit(Boolean.FALSE);
		pStatement = connection.prepareStatement("select name from a");
		resultSet = pStatement.executeQuery();
		while(resultSet.next()){
			String name = resultSet.getString("name");
		}
                java.sql.Timestamp timestamp = 
                    new java.sql.Timestamp(new Date().getTime());
                // 回写中间表信息
		String updateMess = "UPDATE " + MiddleTableName.ORDERACCEPTNAME + 
		" SET STATUS = ?, EXCEPTION_MESS = ?,BACK_TIME=? WHERE ID = ?";
		upStatement = connection.prepareStatement(updateMess);
		if (haveExce) {
			if ("NO_EXIST".equals(errerType)) {
				upStatement.setInt(1, 4);
			} else {
				upStatement.setInt(1, 0);
			}
					
		} else {
			upStatement.setInt(1, 3);
		}
		upStatement.setString(2, exceDesc);
		upStatement.setTimestamp(3,timestamp);
		upStatement.setLong(4, id);
				
		upStatement.execute();
		connection.commit();
		if (null != upStatement) {
			upStatement.close();
		}
	}catch(Exception e) {
		System.out.println(e.getLocalizedMessage());
		try {
			connection.rollback();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}finally{
		free(resultSet, pStatement, connection);
	}
}

public Connection getConnection() {
	Connection conn = null;
	DataSource ds = DataSourceFactory.getDataSourceFactory("middleTble");
	
	try {
		conn = ds.getConnection();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	return conn;
}
//关闭资源
public void free(ResultSet resultSet, Statement statement, 
	Connection connection) {
		if (null != resultSet) {
			try {
			      resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (null != statement) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} finally {
						try {
							if (null != connection) {
								connection.close();
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}else{
			if (null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					try {
						if (null != connection) {
							connection.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}else{
				try {
					if (null != connection) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

 数据源配置文件xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
        "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
      <bean id="middleTble" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close">
		<property name="driverClassName">
            <value>oracle.jdbc.OracleDriver</value>
        </property>
         <property name="url">
         	<value>jdbc:oracle:thin:@192.168.12.111:1521:orcl</value> 
        </property>
        <property name="username">
            <value>middleTable</value>
        </property>
        <property name="password">
            <value>middleTable</value>
        </property> 
		<!--    连接池启动时的初始值 -->
		<property name="initialSize" value="30"/>
		<!-- 	连接池的最大值 -->
	 	<property name="maxActive" value="500"/> 
		<!-- 	最大空闲值.当经过一个高峰时间后,
连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
		<property name="maxIdle" value="2"/>
		<!-- 	 最小空闲值.当空闲的连接数少于阀值时,
连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
		<property name="minIdle" value="1"/>
     </bean>
</beans>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics