栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Hibernate 4.2和Spring 3.1.1设置MultiTenantConnectionProvider

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Hibernate 4.2和Spring 3.1.1设置MultiTenantConnectionProvider

总结一下,这就是我的以下内容。我使用一个简单的CurrentTenantIdentifierResolver。并且没有尝试将数据源从其他地方注入到我的MultiTenantConnectionProviderImpl中,而是在ConnectionProvider中创建了DataSource(c3p0
ComboPooledDatasource),并仅使用了由我的ConnectionProvider提供的连接。因此,我消除了多余的DataSource。为了使DataSource的属性易于配置,我选择从属性文件中获取配置数据。

CurrentTenantIdentifierResolverImpl:

public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {        @Override    public String resolveCurrentTenantIdentifier() {        if (FacesContext.getCurrentInstance() != null){ return FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();        } else { return null;        }    }    @Override    public boolean validateExistingCurrentSessions() {        return true;    }}

MultiTenantConnectionProviderImpl:

请注意,PropertyUtil只是一个简单的本地帮助程序类,用于获取我的属性。由于没有什么特别之处,因此我不会将其包括在内以免弄乱答案。

public class MultiTenantConnectionProviderImpl implements MultiTenantConnectionProvider  {    private static final long serialVersionUID = 8074002161278796379L;    private static Logger log = LoggerFactory.getLogger(MultiTenantConnectionProviderImpl.class );    private ComboPooledDataSource cpds;    private Properties properties;        public MultiTenantConnectionProviderImpl() throws PropertyVetoException {        log.info("Initializing Connection Pool!");        properties = new Properties();        try { properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));        } catch (IOException e) { throw new RuntimeException(e);        }        cpds = new ComboPooledDataSource("Example");        cpds.setDriverClass(properties.getProperty("jdbc.driver"));        cpds.setJdbcUrl(properties.getProperty("jdbc.url"));        cpds.setUser(properties.getProperty("jdbc.user"));        cpds.setPassword(PropertyUtil.getCredential("jdbc.password"));        log.info("Connection Pool initialised!");    }    @Override    public Connection getAnyConnection() throws SQLException {        log.debug("Get Default Connection:::Number of connections (max: busy - idle): {} : {} - {}",new int[]{cpds.getMaxPoolSize(),cpds.getNumBusyConnectionsAllUsers(),cpds.getNumIdleConnectionsAllUsers()});        if (cpds.getNumConnectionsAllUsers() == cpds.getMaxPoolSize()){ log.warn("Maximum number of connections opened");        }        if (cpds.getNumConnectionsAllUsers() == cpds.getMaxPoolSize() && cpds.getNumIdleConnectionsAllUsers()==0){ log.error("Connection pool empty!");        }        return cpds.getConnection();    }    @Override    public Connection getConnection(String tenantIdentifier) throws SQLException {        log.debug("Get {} Connection:::Number of connections (max: busy - idle): {} : {} - {}",new Object[]{tenantIdentifier, cpds.getMaxPoolSize(),cpds.getNumBusyConnectionsAllUsers(),cpds.getNumIdleConnectionsAllUsers()});        if (cpds.getNumConnectionsAllUsers() == cpds.getMaxPoolSize()){ log.warn("Maximum number of connections opened");        }        if (cpds.getNumConnectionsAllUsers() == cpds.getMaxPoolSize() && cpds.getNumIdleConnectionsAllUsers()==0){ log.error("Connection pool empty!");        }        return cpds.getConnection(tenantIdentifier, PropertyUtil.getCredential(tenantIdentifier));    }    @Override    public void releaseAnyConnection(Connection connection) throws SQLException {        connection.close();    }    @Override    public void releaseConnection(String tenantIdentifier, Connection connection){        try { this.releaseAnyConnection(connection);        } catch (SQLException e) { throw new RuntimeException(e);        }    }    @Override    public boolean supportsAggressiveRelease() {        return false;    }    @SuppressWarnings("rawtypes")    @Override    public boolean isUnwrappableAs(Class unwrapType) {        return ConnectionProvider.class.equals( unwrapType ) || MultiTenantConnectionProvider.class.equals( unwrapType ) || MultiTenantConnectionProviderImpl.class.isAssignableFrom( unwrapType );    }    @SuppressWarnings("unchecked")    @Override    public <T> T unwrap(Class<T> unwrapType) {        if ( isUnwrappableAs( unwrapType ) ) { return (T) this;        }        else { throw new UnknownUnwrapTypeException( unwrapType );        }    }}

特定于 c3p0的 配置来自 c3p0-config.xml

<c3p0-config>    <named-config name="Example">        <property name="acquireIncrement">3</property>        <property name="preferredTestQuery">SELECT 1</property>        <property name="checkoutTimeout">2000</property>        <property name="idleConnectionTestPeriod">30</property>        <property name="initialPoolSize">1</property>        <property name="maxIdleTime">18000</property>        <property name="maxPoolSize">30</property>        <property name="minPoolSize">1</property>        <property name="maxStatements">50</property>        <property name="testConnectionOnCheckin">true</property>    </named-config></c3p0-config>

db特定的属性由 config.properties 文件提供:

jdbc.url=<serverUrl>jdbc.driver=<driverClass>jdbc.dbName=<dBname>jdbc.dbowner=<dbo>jdbc.username=<user>jdbc.password=<password>hibernate.dialect=<hibernateDialect>hibernate.debug=false

从另一个文件以类似的方式获取凭据。

任何提供改进的反馈意见表示赞赏。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号