📄 jndidatasourcefactory.java
字号:
package lib.commons.dal;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import lib.commons.Utils;
public class JNDIDataSourceFactory extends DbAccessFactory {
private String jndiName;
private DataSource dataSource;
private Object lockObject = new Object();
public JNDIDataSourceFactory(String jndiName) {
super(null);
this.jndiName = jndiName;
}
public JNDIDataSourceFactory(Properties properties) {
super(properties);
jndiName = properties.getProperty("dataSourceJndiName");
}
public static Object getObjectFromJNDI(String jndiName)
throws NamingException {
Context ctx = new InitialContext();
Object jndiObject = ctx.lookup(jndiName);
return jndiObject;
}
protected DbAccess createDbAccess() {
if (null == dataSource) {
synchronized (lockObject) {
if (null == dataSource) {
try {
dataSource = (DataSource) getObjectFromJNDI(jndiName);
} catch (NamingException err) {
Log log = Utils.getLog(JNDIDataSourceFactory.class);
log.error(err.getMessage(), err);
}
}
}
}
DbAccess db = null;
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException err) {
Log log = Utils.getLog(JNDIDataSourceFactory.class);
log.error(err.getMessage(), err);
}
db = new DbAccess(conn);
return db;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -