从URL和驱动程序获取数据源

时间:2011-12-18 10:21:19

标签: java url jboss datasource driver

我需要初始化一个给定url和驱动程序的DataSource。 它需要足够通用以支持Oracle和SQL Server。 该应用程序正在jboss 5上运行。

为两者提供样本:

<db-connection name="TEST-ORACLE">
<url>jdbc:oracle:thin:@test:1521:ins1</url>
<driver>oracle.jdbc.xa.client.OracleXADataSource</driver>
<user>user</user>
<password>{ENCR}oRloKFKlqXs=</password>
<min-size>5</min-size>
<max-size>30</max-size>
<idle-timeout-minutes>1</idle-timeout-minutes>
</db-connection>

<db-connections>
    <db-connection name="TEST-MSSQLSERVER">
    <url>jdbc:jtds:sqlserver://server:1433/db;ProgramName=program;SelectMethod=cursor;useLOBs=false</url>
    <driver>net.sourceforge.jtds.jdbc.Driver</driver>
    <user>user</user>
    <password>{ENCR}oRloKFKlqXs=</password>
    <min-size>40</min-size>
    <max-size>80</max-size>
    <idle-timeout-minutes>1</idle-timeout-minutes>
</db-connection>

OracleXADataSource正在实现接口DataSource,因此它很容易退出......但我不确定它是否足够通用。

修改

我想知道如果我在这些连接中有多重数据库连接和多重实例/模式,那么应该采取什么方式呢?

我当前的代码如下:

private DataSource getDataSourceForTanent(TenantConfig i_Tenant) {
        DataSource result = null;
        ClassLoader loader = DBConnector.class.getClassLoader();
        try {
            Class driverClass = loader.loadClass(i_Tenant.getDriver());
            Object driver = driverClass.newInstance();
            if(driver instanceof OracleDataSource){
                ((OracleDataSource)driver).setURL(i_Tenant.getUrl());
                ((OracleDataSource)driver).setUser(i_Tenant.getUser());
                ((OracleDataSource)driver).setPassword(i_Tenant.getPassword());
                result = (DataSource) driver;
            } else if(driver instanceof Driver){
                Properties prop = new Properties();
                prop.put("user", i_Tenant.getUser());
                prop.put("password", i_Tenant.getPassword());
                prop.put("min-size", i_Tenant.getMinSize());
                prop.put("max-size", i_Tenant.getMaxSize());
                prop.put("idle-timeout-minutes", i_Tenant.getIdleTimeoutMinute());
                Connection con = ((Driver)driver).connect(i_Tenant.getUrl(), prop);
                if(con != null){
                    //TODO: SQLServer handling
                }
            }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

TenantConfig是一个包含xml属性的java bean。

0 个答案:

没有答案