jboss中连接池的相关配置

一、中间件配置
主要配置在JBOSS服务器的server/default/deploy目录下oracle-ds.xml文件。主要参数说明:

local-tx-datasource:本地数据源配置根标记名;
jndi-name:数据源JNDI名称,对应Application.properties文件中配置;
driver-class:数据库连接驱动类;
connection-url:数据库连接URL字符串;
user-name:数据库连接用户名;
password:数据库连接密码;
min-pool-size:连接池可激活最小连接数;
max-pool-size:连接池可激活最大连接数;
blocking-timeout-millis:抛出异常前最大的等待连接时间,单位毫秒;
idle-timeout-minutes:连接池已激活的空闲连接超时时间,单位秒。
二、事务管理机制


EJB容器初始化数据持久化环境SessionContext;
用户调用某一xxxSessionBean中的事务方法;
xxxSessionBean调用某一业务逻辑xxxLogic操作;
xxxLogic调用DAO层存取数据;
当2~4过程中出现异常,sessionContext调用方法setRollbbackonly来捕捉异常,并且把该异常转化为EJBException(继承自RuntimeException)继续抛出,同时回滚事务。
三、事务管理配置
由JBOSS的EJB容器管理事务,一般是配置在JBOSS的server/default/conf目录下jboss-service.xml文件中。具体配置参数说明如下:
<mbean code="org.jboss.tm.TransactionManagerService"
name="jboss:service=TransactionManager"
xmbean-dd="resource:xmdesc/TransactionManagerService-xmbean.xml">
<!-- 容器管理事务超时时间,单位秒 -->
<attribute name="TransactionTimeout">300</attribute>
<!-- 容器管理事务的全局标示。值为true表示启用全局性事务,false表示不启用 -->
<attribute name="GlobalIdsEnabled">true</attribute>
<depends optional-attribute-name="XidFactory">jboss:service=XidFactory</depends>
</mbean>

四、JAVA代码获取数据库连接
[java] view plaincopyprint?
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<String, DataSource> datasourceTable = new Hashtable<String, DataSource>();

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);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值