论坛首页 Java企业应用论坛

bboss持久层多数据源配置及使用方法

浏览 2441 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-05-11   最后修改:2014-11-21
bboss持久层多数据源配置及使用方法

1.配置多个数据源-poolman.xml
<poolman>

  <datasource>

       <dbname>ds0</dbname>
	<loadmetadata>false</loadmetadata>
    <jndiName>ds0_datasource_jndiname</jndiName>
     <autoprimarykey>false</autoprimarykey>
     <cachequerymetadata>false</cachequerymetadata>
    <driver>oracle.jdbc.driver.OracleDriver</driver>

     <url>jdbc:oracle:thin:@//10.0.15.134:1521/orcl</url> 
    <username>ddd</username>
    <password>sanygcmp</password>

    <txIsolationLevel>READ_COMMITTED</txIsolationLevel>

    <nativeResults>true</nativeResults>

    <poolPreparedStatements>false</poolPreparedStatements>

    <initialConnections>2</initialConnections>
    
    <minimumSize>10</minimumSize>
    <maximumSize>10</maximumSize>
	<!--控制connection达到maximumSize是否允许再创建新的connection
		true:允许,缺省值
		false:不允许-->
    <maximumSoft>false</maximumSoft>
    
    <!-- 
    是否检测超时链接(事务超时链接)
    true-检测,如果检测到有事务超时的链接,系统将强制回收(释放)该链接
    false-不检测,默认值
     -->
    <removeAbandoned>false</removeAbandoned>
	<!--
		链接使用超时时间(事务超时时间)
		单位:秒
	-->
    <userTimeout>50</userTimeout>
    <!-- 
    	系统强制回收链接时,是否输出后台日志
    	true-输出,默认值
    	false-不输出
     -->
    <logAbandoned>false</logAbandoned>
    
    <!-- 
    	数据库会话是否是readonly,缺省为false
     -->
    <readOnly>true</readOnly>
	
	<!--
		对应属性:timeBetweenEvictionRunsMillis
		the amount of time (in milliseconds) to sleep between examining idle objects for eviction 
	-->
	<skimmerFrequency>120000</skimmerFrequency>
	<!--对应于minEvictableIdleTimeMillis 属性:
	minEvictableIdleTimeMillis the minimum number of milliseconds 
	an object can sit idle in the pool before it is eligable for evcition
	单位:秒
	
	空闲链接回收时间,空闲时间超过指定的值时,将被回收
	-->
	<connectionTimeout>240000</connectionTimeout>
	<!--
	numTestsPerEvictionRun 
	the number of idle objects to 
	examine per run within the idle object eviction thread (if any)
	
	每次回收的链接个数 
	-->
    <shrinkBy>5</shrinkBy>
    <testWhileidle>true</testWhileidle>
    <keygenerate>composite</keygenerate>
    <debugging>true</debugging>-->
    <!-- 请求链接时等待时间,单位:秒
    客服端程序请求链接等待时间超过指定值时,后台包等待超时异常
     -->
    <maxWait>60</maxWait>
    <validationQuery>select 1 from dual</validationQuery>

  </datasource>
  
  
  <datasource>

    <dbname>ds1</dbname>
	<loadmetadata>false</loadmetadata>
    <jndiName>ds1_datasource_jndiname</jndiName>
     <autoprimarykey>false</autoprimarykey>
     <cachequerymetadata>false</cachequerymetadata>
    <driver>oracle.jdbc.driver.OracleDriver</driver>

     <url>jdbc:oracle:thin:@//10.0.15.134:1521/orcl</url> 
    <username>sanygcmp</username>
    <password>sanygcmp</password>

    <txIsolationLevel>READ_COMMITTED</txIsolationLevel>

    <nativeResults>true</nativeResults>

    <poolPreparedStatements>false</poolPreparedStatements>

    <initialConnections>2</initialConnections>
    
    <minimumSize>10</minimumSize>
    <maximumSize>10</maximumSize>
	<!--控制connection达到maximumSize是否允许再创建新的connection
		true:允许,缺省值
		false:不允许-->
    <maximumSoft>false</maximumSoft>
    
    <!-- 
    是否检测超时链接(事务超时链接)
    true-检测,如果检测到有事务超时的链接,系统将强制回收(释放)该链接
    false-不检测,默认值
     -->
    <removeAbandoned>false</removeAbandoned>
	<!--
		链接使用超时时间(事务超时时间)
		单位:秒
	-->
    <userTimeout>50</userTimeout>
    <!-- 
    	系统强制回收链接时,是否输出后台日志
    	true-输出,默认值
    	false-不输出
     -->
    <logAbandoned>false</logAbandoned>
    
    <!-- 
    	数据库会话是否是readonly,缺省为false
     -->
    <readOnly>true</readOnly>
	
	<!--
		对应属性:timeBetweenEvictionRunsMillis
		the amount of time (in milliseconds) to sleep between examining idle objects for eviction 
	-->
	<skimmerFrequency>120000</skimmerFrequency>
	<!--对应于minEvictableIdleTimeMillis 属性:
	minEvictableIdleTimeMillis the minimum number of milliseconds 
	an object can sit idle in the pool before it is eligable for evcition
	单位:秒
	
	空闲链接回收时间,空闲时间超过指定的值时,将被回收
	-->
	<connectionTimeout>240000</connectionTimeout>
	<!--
	numTestsPerEvictionRun 
	the number of idle objects to 
	examine per run within the idle object eviction thread (if any)
	
	每次回收的链接个数 
	-->
    <shrinkBy>5</shrinkBy>
    <testWhileidle>true</testWhileidle>
    <keygenerate>composite</keygenerate>
    <debugging>true</debugging>-->
    <!-- 请求链接时等待时间,单位:秒
    客服端程序请求链接等待时间超过指定值时,后台包等待超时异常
     -->
    <maxWait>60</maxWait>
    <validationQuery>select 1 from dual</validationQuery>

  </datasource> 
</poolman>

配置了两个数据源ds0和ds1,下面介绍利用持久层api来执行不同数据源的db操作。

2.多数据源api实例
目前bboss常用的数据库访问组件ConfigSQLExecutor和SQLExecutor都提供了多数据源操作api,这种api中都显示地指定dbname参数,也就是数据源的名称,例如上面的ds0和ds1;另外一种api是不带dbname参数,这种api默认在poolman.xml文件中配置的第一个数据源上执行db操作。两种api举例说明如下(以SQLExecutor组件为例来说明,ConfigSQLExecutor组件除了sql语句从配置文件中读取外,它的api与SQLExecutor组件api一致,就不额外介绍ConfigSQLExecutor了。):

TransactionManager tm = new TransactionManager();
try{
tm.begin();//开始事务
SQLExecutor.delete("delete from LISTBEAN where id=?",1);//不带数据源dbname的api,默认在第一个数据源上执行db操作,也就是ds0数据源。
		SQLExecutor.deleteWithDBName("ds0","delete from LISTBEAN where id=?",1);//显示指定db操作在ds0数据源上操作
		SQLExecutor.deleteWithDBName("ds1","delete from LISTBEAN where id=?",1);//显示指定db操作在ds1数据源上操作
tm.commit();//提交事务
}
catch(Exception e)
{
   throw e;
}
finally
{
   tm.release();//释放事务资源,如果有异常发生,则会回滚事务
}

上面是以删除操作来做说明,同时我们将两个数据源上的数据库操作包含到一个事务中。其它操作(插入,修改,查询)类似也不举例一一介绍了,具体可参考以下示例文件:
SimpleApiTest1.java
ConfigSQLExecutorTest.java
SimpleApiTest.java
TestPrepareDBUtilNewInterface.java
论坛首页 Java企业应用版

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