在切换DataSource和选择模式时,Hibernate是否会产生多租户开销

时间:2018-06-07 08:29:56

标签: java hibernate jpa multi-tenant

在下面给出的代码中,我试图在tenantIdentifier的基础上更改数据源和模式。

例如:

@Override
    protected DataSource selectDataSource(String tenantIdentifier) {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup(JNDI_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Override
    public Connection getConnection(String tenantIdentifier) throws SQLException {
        final Connection connection = selectDataSource(tenantIdentifier).getConnection();
        try {
            connection.createStatement().execute("select now()");
            connection.createStatement().execute("USE " + tenantIdentifier);
        } catch (final SQLException e) {
            throw new HibernateException("Error trying to alter schema [" + tenantIdentifier + "]", e);
        }
        return connection;
    }

1 个答案:

答案 0 :(得分:0)

切换DataSource和架构期间的小开销(以ms为单位)。