我想实现动态jndi数据源,能这样实现吗
首先,我在jboss中动态配置几个数据源,如jndi1,jndi2,jndi3。
这些数据源对应于oracle数据库的不同用户,但这些用户的表结构均相同,只是数据年份不同,由于数据量比较大,将其存放于不同的oracle用户中。
然后,在 同一个 web网站上 根据不同用户登录方式选择不同的数据源,以实现。注意是同一个web网站,也就是说相同的war包。
我想的是增加一个全局过滤器filter,按照用户登录选择的数据年份,在过滤器中自动切换数据源的名称。
请问这样做是可以的吗?
该如何实现呢?
在线等待中,先谢谢了
[解决办法]
你的想法很好。是可以实现的。
使用JBOSS初始化三个连接池也就是你说的JNDI数据源。
然后将这三个连接池分别命名 如 :J1 J2 J3
当用户选择年份后判断应选哪个数据源,最后处理就可以了。
[解决办法]
当然可以,配一个-ds.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<!-- See http://www.jboss.org/community/wiki/Multiple1PC for information
about local-tx-datasource -->
<!-- $Id: mysql-ds.xml 97536 2009-12-08 14:05:07Z jesper.pedersen $ -->
<!-- Datasource config for MySQL using 3.0.9 available from: http://www.mysql.com/downloads/api-jdbc-stable.html -->
<datasources>
<local-tx-datasource>
<jndi-name>DS.xx</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:oracle:thin:@10.xx.xx.xx:1521:xx</connection-url>
<!-- Here are a couple of the possible OCI configurations. For more information,
see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url> or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly. -->
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>xxx</user-name>
<password>xxx</password>
<!-- Uses the pingDatabase method to check a connection is still valid
before handing it out from the pool -->
<!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name -->
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
</exception-sorter-class-name>
<!-- sql to call when connection is created <new-connection-sql>some arbitrary
sql</new-connection-sql> -->
<!-- sql to call on an existing pooled connection when it is obtained from
pool - the OracleValidConnectionChecker is prefered <check-valid-connection-sql>some
arbitrary sql</check-valid-connection-sql> -->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>DS.xx</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:oracle:thin:@10.xx.xx.xx:1521:xxx</connection-url>
<!-- Here are a couple of the possible OCI configurations. For more information,
see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url> or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly. -->
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>xxx</user-name>
<password>xxxx</password>
<!-- Uses the pingDatabase method to check a connection is still valid
before handing it out from the pool -->
<!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name -->
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
</exception-sorter-class-name>
<!-- sql to call when connection is created <new-connection-sql>some arbitrary
sql</new-connection-sql> -->
<!-- sql to call on an existing pooled connection when it is obtained from
pool - the OracleValidConnectionChecker is prefered <check-valid-connection-sql>some
arbitrary sql</check-valid-connection-sql> -->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>