【发布时间】:2017-05-15 02:46:52
【问题描述】:
我有一个 Spring bean,它引用连接工厂来访问 J2C 资源。此代码部署在 2 个负载均衡的 jvm 中的 WebSphere 服务器中。我在 bean xml 中有 readAuth 方法作为 init-method,但在负载测试期间它被多次调用。由于 Spring 单例是每个容器的每个 bean,我假设有多个容器导致它多次加载。所以我删除了 init-method 并将用户名和密码更改为 static 并在 get 方法中添加了 null 检查。但现在,readAuth 方法也被多次调用。我想确保每个 jvm 只调用一次此方法,因为此方法访问服务器资源并且连接在负载测试期间超时。 请就如何编写此类提出最佳方法。提前致谢。
<bean id="J2CUtils"
class="test.J2CUtils">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<jee:jndi-lookup id="connectionFactory" jndi-name="eis/J2CAuth" />
public class J2Ctils {
private ConnectionFactory connectionFactory;
private static String userName;
private static String password;
private void readAuth() throws ResourceException {
System.out.println("Auth loaded");
Connection conn = connectionFactory.getConnection();
Interaction interaction = (Interaction) conn.createInteraction();
Config config = interaction.getConfig();
userName = config.getUserName();
password = config.getPassword();
}
public String getUserName() {
if(null == userName) {
readAuth();
}
return userName;
}
public String getPassword() {
if(null == password) {
readAuth();
}
return password;
}
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
}
【问题讨论】:
标签: spring ioc-container