在获得连接之前,自动更改上下文上下文
我正在使用 Spring 多租户框架,并且尝试根据每个实体类中的属性设置 TenanContext。 我使用的拦截器不会在每次事务之前设置TenantSelector,并且我没有找到任何方法来实现它(onLoad在应用程序加载期间而不是之前执行所有操作每笔交易)。我正在尝试来自 org.hibernate.EmptyInterceptor 的其他拦截器,但任何拦截器都可以满足我的需要。
这是我使用的拦截器:
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setEntityInterceptor(new MultitenancyInterceptor());
及其实现:
public class MultitenancyInterceptor extends EmptyInterceptor {
@Override
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
//logic to update the TenantContext
return super.onLoad(entity, id, state, propertyNames, types);
}
}
是否有任何拦截器可以帮助我在每次从 AbstractMultiTenantConnectionProvider 获取连接之前计算 TenantContext?
public abstract class MultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {
...
@Override
public Connection getConnection(String tenantIdentifier)
throws SQLException {
Connection connection = super.getConnection(tenantIdentifier);
connection.createStatement()
.execute(String.format("USE `%s`;", tenantIdentifier));
return connection;
}
}
I'm working with Spring multitenancy framework, and i'm trying to set the TenanContext depending on a property in each Entity class.
The interceptor that I use doesn't set the TenantSelector before each transaction, and I don't find any to achieve it (onLoad do everything during the application load but not before each transaction). I'm trying other interceptors from org.hibernate.EmptyInterceptor but any do what I need.
This is the interceptor that I use:
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setEntityInterceptor(new MultitenancyInterceptor());
And its implementation:
public class MultitenancyInterceptor extends EmptyInterceptor {
@Override
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
//logic to update the TenantContext
return super.onLoad(entity, id, state, propertyNames, types);
}
}
Is there any interceptor which can help me to calculate the TenantContext before each getConnection from AbstractMultiTenantConnectionProvider?
public abstract class MultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {
...
@Override
public Connection getConnection(String tenantIdentifier)
throws SQLException {
Connection connection = super.getConnection(tenantIdentifier);
connection.createStatement()
.execute(String.format("USE `%s`;", tenantIdentifier));
return connection;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论