jboss 连接池配置

一、中间件配置

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

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

  1. EJB容器初始化数据持久化环境SessionContext;
  2. 用户调用某一xxxSessionBean中的事务方法;
  3. xxxSessionBean调用某一业务逻辑xxxLogic操作;
  4. xxxLogic调用DAO层存取数据;
  5. 当2~4过程中出现异常,sessionContext调用方法setRollbbackonly来捕捉异常,并且把该异常转化为EJBException(继承自RuntimeException)继续抛出,同时回滚事务。
三、事务管理配置

由JBOSS的EJB容器管理事务,一般是配置在JBOSS的server/default/conf目录下jboss-service.xml文件中。具体配置参数说明如下:
      name="jboss:service=TransactionManager"
      xmbean-dd="resource:xmdesc/TransactionManagerService-xmbean.xml">
     
      300
     
      true
      jboss:service=XidFactory

四、JAVA代码获取数据库连接
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); 
        } 
    } 
}


 

你可能感兴趣的:(Java,EE)