`
tianyihuyidao9
  • 浏览: 165888 次
  • 性别: Icon_minigender_1
  • 来自: 济南
文章分类
社区版块
存档分类
最新评论

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);  
        }  
    }  

分享到:
评论

相关推荐

    在Eclipse中使用JBoss数据库连接池技术

    在Eclipse中使用JBoss数据库连接池技术,主要是为了提高应用程序的性能和资源管理效率。数据库连接池技术允许...通过上述步骤,你可以在Eclipse中配置和使用JBoss连接池连接到MySQL数据库,实现更加优化的数据访问。

    jboss配置Oracle连接池.doc

    在JBoss服务器中配置Oracle数据库连接池是一项重要的任务,它能够有效地管理应用程序对数据库的访问,提高系统的性能和响应速度。以下是根据提供的文档内容进行的知识点详细解析。 #### 二、创建oracle-ds.xml文件 ...

    jboss配置 mysql数据库连接池

    本文将详细介绍如何在jBoss应用服务器中配置MySQL数据库连接池,以确保应用程序能够稳定高效地与数据库交互。 #### 二、准备工作 为了能够在jBoss中成功配置MySQL数据库连接池,我们需要进行一系列的准备工作: 1....

    tomcat、jboss 连接池配置

    ### JBoss 连接池配置 JBoss AS (Application Server) 是另一个流行的Java应用服务器。下面介绍如何在JBoss上配置连接池。 #### 1. 添加JDBC驱动 在JBoss的`modules`目录下创建一个新的模块,用来存放JDBC驱动。...

    j2ee开发连接池的配置

    - 添加依赖:首先,你需要在项目中引入相应的连接池库,例如在Maven或Gradle的依赖配置中添加对应的连接池库。 - 配置数据源:创建一个数据源配置文件,通常为XML格式,配置包括数据库URL、用户名、密码、最大连接...

    在JBoss中配置多个数据库和数据源

    "在JBoss中配置多个数据库和数据源" 在JBoss中配置多个数据库和数据源是非常复杂的,因为...* 配置数据源的步骤:配置新数据源、定义连接池的参数 * 使用JNDI名称来引用数据源 * 添加另一个数据源需要遵循相同的步骤

    proxool连接池配置详解

    ### Proxool连接池配置详解 #### 一、概述 Proxool是一个开源的轻量级Java数据库连接池实现,其主要目标是替代常见的数据库连接池解决方案,如C3P0或DBCP,并且提供了更加灵活和易于配置的特性。在实际应用中,...

    连接池的配置

    7. **用户名和密码**:用于连接数据库的凭证,通常在连接池的配置中以加密或安全的方式存储。 8. **事务隔离级别**:定义了事务间的隔离程度,如READ_UNCOMMITTED、READ_COMMITTED、REPEATABLE_READ、SERIALIZABLE...

    JBOSS\Tomcat最大连接数配置和jvm内存配置.docx

    在`deploy/jboss-web.deployer/server.xml`文件中,`&lt;Connector&gt;`标签用于定义HTTP/HTTPS连接器,其内包含若干属性用于控制连接池的行为。`maxThreads`参数定义了服务器可以同时处理的最大线程数,即并发请求的最大...

    Jboss + 优化配置

    综上所述,通过对JBoss的后台启动方式、内存管理、日志输出以及数据库连接池等方面的优化,不仅可以提高系统的稳定性和性能,还能有效降低运维成本,提升用户体验。这些优化措施应根据具体的应用场景和需求进行调整...

    JBOSS4数据源配置大全

    通过对EJB部署描述符、JDBC驱动程序、数据源配置文件及连接池参数的合理设置,可以有效地实现不同数据库与JBoss应用服务器的集成。这不仅有助于提高系统的性能和稳定性,还能满足不同场景下的业务需求。

    Jboss配置全面解析

    此外,还有其他配置,如数据库连接池配置(`server/default/conf/jboss-service.xml`中的`MBean`)、EJB配置(`server/default/conf/jboss-ejb3.xml`)、邮件服务配置(`server/default/conf/jboss-mail.xml`)等。...

    在jboss连接oracle

    上述配置中的`connection-url`是你的Oracle数据库连接字符串,`user-name`和`password`是数据库登录凭证,`pool`部分定义了连接池的参数,`drivers`部分配置了Oracle驱动的相关信息。 4. **测试连接**: 保存配置...

    jboss配置MySql的JNDI

    - **连接池优化**:根据应用需求调整连接池参数,如最大活动连接数、最小空闲连接数等,以提升性能。 - **健康检查**:考虑启用连接有效性检查机制,如`valid-connection-checker-class-name`和`check-valid-...

    jboss相关技术文档

    在Jboss3.0和Tomcat4.03的配置中,主要涉及配置文件的修改,例如`server.xml`,以设定虚拟主机、上下文路径和数据源。整合后的系统能够同时利用JBoss的强大EJB支持和Tomcat的轻量级特性。 **5. JBoss与MS SQL ...

    JBOSS配置.rarJBOSS配置.rarJBOSS配置.rarJBOSS配置.rar

    此外,还可以配置连接池参数,如最小连接数、最大连接数、超时时间等。 3. **安全管理**:JBOSS的安全管理包括用户认证和授权。认证通常通过`management-users.properties`和`application-users.properties`文件...

    Jboss 优化配置.

    数据库连接池的合理配置可以极大地提高应用程序访问数据库的速度。JBoss支持多种数据库连接池,如C3P0、DBCP等。通过调整连接池的最大活动连接数、最大空闲连接数等参数,可以使数据库访问更加高效。 #### 五、...

    配置jboss数据源

    在上述配置中,我们使用了MySQL的驱动,所以要在`&lt;drivers&gt;`节点下添加对应的驱动配置,例如: ```xml &lt;xa-datasource-class&gt;com.mysql.jdbc.Driver&lt;/xa-datasource-class&gt; ``` 4. **启动和测试**:...

    JBoss安全优化

    然后将签署后的证书导入到服务器的keystore中,并在服务器配置中指定SSL端口和证书信息。 对于检测是否设置定时登出,这是为了防止用户长时间无操作导致系统被他人占用。在JBoss的管理控制台或者自定义应用中,可以...

    JBOSS_5.0.1数据库配置文件及相应的JDBC

    在实际应用中,根据项目需求,可能还需要处理事务隔离级别、连接池特性、异常处理策略等高级配置。理解并熟练掌握这些配置对确保应用程序的稳定性和性能至关重要。 总的来说,JBoss 5.0.1的数据库配置文件及相应的...

Global site tag (gtag.js) - Google Analytics