gpt4 book ai didi

java - 制作静态数据源

转载 作者:行者123 更新时间:2023-11-30 05:29:41 26 4
gpt4 key购买 nike

我对这个主题做了足够的研究,但尚未找到具体的答案。以下是代码片段,

private static Map<String,DataSource> dataSourceMap;

static {
Map<String,DataSource> jndiContextDataSourceMap = new HashMap<>();
try {
jndiContextDataSourceMap.put(JNDI_DEFAULT_DATASOURCE, (DataSource) new InitialContext().lookup(JNDI_DEFAULT_DATASOURCE));
} catch (NamingException namingException) {
logger.error("Unable to obtain default DataSource through JNDI Lookup.", namingException);
}
try {
jndiContextDataSourceMap.put(JNDI_READ_REPLICA, (DataSource) new InitialContext().lookup(JNDI_READ_REPLICA));
} catch (NamingException namingException) {
logger.error("Unable to obtain read only DataSource through JNDI Lookup.", namingException);
}
dataSourceMap = Collections.unmodifiableMap(jndiContextDataSourceMap);
}

使用相同的DataSource对象可以吗?我检查了docs ,但这实际上不包含线程安全的细节。

我使用它来避免查找并避免为每个请求创建新的InitialContext

最佳答案

是的,应该没问题。由于DataSource 负责提供Connection,因此不使其线程安全将是一个非常糟糕的主意。毕竟,大多数程序同时使用多个连接。它可能没有被记录为线程安全并且 it's not specified that it should be thread-safe ,但任何理智实现都是线程安全的。

关于java - 制作静态数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com