主要配置在JBOSS服务器的server/default/deploy目录下oracle-ds.xml文件。主要参数说明:
由JBOSS的EJB容器管理事务,一般是配置在JBOSS的server/default/conf目录下jboss-service.xml文件中。具体配置参数说明如下:
xmbean-dd="resource:xmdesc/TransactionManagerService-xmbean.xml">
package com.sunrise.www.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sunrise.www.utils.exception.DAOException;
public class DAOUtils {
private static final Log log = LogFactory.getLog(DAOUtils.class);
private static boolean datasource_flag = false;
private static boolean read_datasource_flag = true;
private static boolean read_direct_conn_flag = false;
private static String datasource_name = null;
public static Connection getDBConnection(String datasourceName)
throws DAOException {
try {
return ServiceLocator.getInstance().getDataSource(datasourceName).getConnection();
} catch (Exception ex) {
throw new DAOException("Cannot connect to database.", ex);
}
}
public static Connection getDBConnection() throws DAOException {
try {
if (read_datasource_flag) {
String tmp = SystemConfig.getDefaultConfig("USE_DATASOURCE").toLowerCase();
datasource_flag = Boolean.parseBoolean(tmp);
read_datasource_flag = false;
}
Connection conn = null;
if (!datasource_flag) {
String user = "", password = "", url = "", driver = "";
if (!read_direct_conn_flag) {
user = SystemConfig.getDefaultConfig("USER");
password = SystemConfig.getDefaultConfig("PASSWORD");
url = SystemConfig.getDefaultConfig("URL");
driver = SystemConfig.getDefaultConfig("DATABASEDRIVER");
read_direct_conn_flag = true;
}
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(true);
} else {
if (datasource_name == null)
datasource_name = SystemConfig.getDefaultConfig("DATASOURCE_NAME");
conn = getDBConnection(datasource_name);
}
return conn;
} catch (Exception ex) {
log.error("连接数据库出错", ex);
throw new DAOException(ex);
}
}
public static void closeAll(Connection dbConnection,
PreparedStatement stmt, ResultSet result) throws DAOException {
close(result);
close(stmt);
close(dbConnection);
}
public static void close(Connection dbConnection, PreparedStatement stmt)
throws DAOException {
close(stmt);
close(dbConnection);
}
public static void close(Connection dbConnection) throws DAOException {
try {
if (dbConnection != null && !dbConnection.isClosed()) {
dbConnection.close();
dbConnection = null;
}
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "DB connection : /n", se);
}
}
public static void close(ResultSet result) throws DAOException {
try {
if (result != null) result.close();
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "Result Set : /n", se);
}
}
public static void close(PreparedStatement stmt) throws DAOException {
try {
if (stmt != null) stmt.close();
} catch (SQLException se) {
throw new DAOException("SQL Exception while closing " + "Statement : /n", se);
}
}
/**
* @return read_datasource_flag
*/
public static boolean isRead_datasource_flag() {
return read_datasource_flag;
}
/**
* @param read_datasource_flag
* 设置read_datasource_flag
*/
public static void setRead_datasource_flag(boolean read_datasource_flag) {
DAOUtils.read_datasource_flag = read_datasource_flag;
}
}
package com.sunrise.www.utils;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sunrise.www.utils.exception.ServiceLocatorException;
public class ServiceLocator {
private static Log log = LogFactory.getLog(ServiceLocator.class);
private static ServiceLocator serviceLocator;
private static InitialContext context;
private static Hashtable datasourceTable = new Hashtable();
private ServiceLocator() throws ServiceLocatorException {
try {
context = getInitialContext();
} catch (Exception e) {
log.error(e.getMessage());
throw new ServiceLocatorException(e.getMessage());
}
}
/**
* 取得一个服务定位器的实例
* @return
* @throws ServiceLocatorException
*/
public static synchronized ServiceLocator getInstance()
throws ServiceLocatorException {
if (serviceLocator == null)
serviceLocator = new ServiceLocator();
return serviceLocator;
}
/**
* 取J2EE服务器的相关环境变量
* @return
* @throws NamingException
*/
public static InitialContext getInitialContext() throws NamingException {
return new InitialContext();
}
/**
* 取指定的 data source
* @param datasourceName
* @return
* @throws ServiceLocatorException
*/
public DataSource getDataSource(String datasourceName)
throws ServiceLocatorException {
try {
DataSource dataSource = (DataSource) datasourceTable.get(datasourceName);
if (dataSource == null) {
dataSource = (DataSource) context.lookup(actualJndiName(datasourceName));
if (dataSource == null) {
log.error("不能初始化系统对象: " + datasourceName);
throw new ServiceLocatorException("不能初始化系统对象: " + datasourceName);
}
datasourceTable.put(datasourceName, dataSource);
}
return dataSource;
} catch (NamingException ne) {
log.error(ne.getMessage());
throw new ServiceLocatorException(ne);
}
}
private String actualJndiName(String jndiName) throws NamingException {
String prefixes = (String) context.getEnvironment().get(InitialContext.URL_PKG_PREFIXES);
return ((prefixes != null && prefixes.contains("jboss")) ? "java:/" : "") + jndiName;
}
}
package com.sunrise.www.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.sunrise.www.utils.exception.SystemConfigException;
public class SystemConfig {
private static final String DEFAULT_CONFIG = "Application.properties";
private static ClassLoader defaultClassLoader;
private SystemConfig() {
}
/**
* Returns the default classloader (may be null).
*
* @return The default classloader
*/
public static ClassLoader getDefaultClassLoader() {
return defaultClassLoader;
}
/**
* Sets the default classloader
*
* @param defaultClassLoader -
* the new default ClassLoader
*/
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
SystemConfig.defaultClassLoader = defaultClassLoader;
}
/**
* Returns a resource on the classpath as a Properties object
*
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static Properties getResourceAsProperties(String resource)
throws IOException {
Properties props = new Properties();
InputStream in = null;
String propfile = resource;
in = getResourceAsStream(propfile);
props.load(in);
in.close();
return props;
}
/**
* Returns a resource on the classpath as a Properties object
*
* @param loader
* The classloader used to load the resource
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static Properties getResourceAsProperties(ClassLoader loader,
String resource) throws IOException {
Properties props = new Properties();
InputStream in = null;
String propfile = resource;
in = getResourceAsStream(loader, propfile);
props.load(in);
in.close();
return props;
}
/**
* Returns a resource on the classpath as a Stream object
*
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(String resource)
throws IOException {
return getResourceAsStream(getClassLoader(), resource);
}
/**
* Returns a resource on the classpath as a Stream object
*
* @param loader
* The classloader used to load the resource
* @param resource
* The resource to find
* @return The resource
* @throws IOException
* If the resource cannot be found or read
*/
public static InputStream getResourceAsStream(ClassLoader loader,
String resource) throws IOException {
InputStream in = null;
if (loader != null)
in = loader.getResourceAsStream(resource);
if (in == null)
in = ClassLoader.getSystemResourceAsStream(resource);
if (in == null)
throw new IOException("Could not find resource " + resource);
return in;
}
private static ClassLoader getClassLoader() {
if (defaultClassLoader != null) {
return defaultClassLoader;
} else {
return Thread.currentThread().getContextClassLoader();
}
}
// 本行以上代码拷贝自com.ibatis.common.resources.Resources
/**
* 从默认配置文件(Application.properties)中读取配置
*
* @param key
* 键
* @param defaultValue
* 默认值
* @return
*/
public static String getDefaultConfig(String key, String defaultValue) {
return loadProperties(DEFAULT_CONFIG).getProperty(key, defaultValue);
}
public static String getDefaultConfig(String key) {
return loadProperties(DEFAULT_CONFIG).getProperty(key);
}
public static String getConfig(String resource, String key) {
return loadProperties(resource).getProperty(key);
}
public static String getConfig(String resource, String key,
String defaultValue) {
return loadProperties(resource).getProperty(key);
}
public static Properties loadProperties(String resource) {
try {
Properties props = getResourceAsProperties(resource);
if(props != null){
return props;
}
throw new SystemConfigException("Resource " + resource + " is not exist.");
} catch (IOException e) {
throw new SystemConfigException(e);
}
}
}