首页 > 其他分享 >在tomcat与jboss中配置c3p0连接池

在tomcat与jboss中配置c3p0连接池

时间:2023-06-09 10:02:37浏览次数:43  
标签:tomcat -- c3p0 server jboss DataSource your

Appendix D: Configuring c3p0 DataSources in Tomcat

You can easily configure Apache's Tomcat web application server to use c3p0 pooled DataSources. Below is a Tomcat 5.0 sample config to get you started. It's a fragment of Tomcat's conf/server.xml file, which should be modified to suit and placed inside a <Context> element.

<Resource name="jdbc/pooledDS" auth="Container" type="com.mchange.v2.c3p0.ComboPooledDataSource" /> <ResourceParams name="jdbc/pooledDS"> <parameter> <name>factory</name> <value>org.apache.naming.factory.BeanFactory</value> </parameter> <parameter> <name>driverClass</name> <value>org.postgresql.Driver</value> </parameter> <parameter> <name>jdbcUrl</name> <value>jdbc:postgresql://localhost/c3p0-test</value> </parameter> <parameter> <name>user</name> <value>swaldman</value> </parameter> <parameter> <name>password</name> <value>test</value> </parameter> <parameter> <name>minPoolSize</name> <value>5</value> </parameter> <parameter> <name>maxPoolSize</name> <value>15</value> </parameter> <parameter> <name>acquireIncrement</name> <value>5</value> </parameter> </ResourceParams>



For Tomcat 5.5, try something like the following (thanks to Carl F. Hall for the sample!):




<Resource auth="Container" description="DB Connection" driverClass="com.mysql.jdbc.Driver" maxPoolSize="4" minPoolSize="2" acquireIncrement="1" name="jdbc/TestDB" user="test" password="ready2go" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:mysql://localhost:3306/test?autoReconnect=true" />


The rest is standard J2EE stuff: You'll need to declare your DataSource reference in your web.xml file:


<resource-ref> <res-ref-name>jdbc/pooledDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>


And you can access your DataSource from code within your web application like this:


InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/pooledDS");

That's it!


拷贝jdbc.jar和c3p0.jar到defaul/lib/下

Appendix E: JBoss-specific notes

To use c3p0 with JBoss:


Place c3p0's jar file in the lib directory of your jboss server instance (e.g. ${JBOSS_HOME}/server/default/lib)

Modify the file below, and save it as c3p0-service.xml in the deploy directory of your jboss server (e.g. ${JBOSS_HOME}/server/default/deploy). Note that parameters must be capitalized in this file, but otherwise they are defined as described above.

Note: Users of c3p0 jboss support prior to c3p0-0.9.1 please click here!

Please note: As of c3p0-0.9.1, the class name of the jboss configuration mbean has changed to com.mchange.v2.c3p0.jboss.C3P0PooledDataSource (from com.mchange.v2.c3p0.mbean.C3P0PooledDataSource), in order to distinguish what is really jboss-specific functionality from c3p0's more general JMX support.


The old jboss config mbeans are deprecated, but will still work. However, support for new configuration parameters will only be added under the new name. Updating requires a one-word change to your c3p0-service.xml, change "mbean" to "jboss" where your old file says 'code="com.mchange.v2.c3p0.mbean.C3P0PooledDataSource"'. Just do it!


Hide box.以下是0.9.2配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>

<server>

   <mbean code="com.mchange.v2.c3p0.mbean.C3P0PooledDataSource"
          name="jboss:service=C3P0PooledDataSource">
     
      <attribute name="JndiName">java:PooledDS</attribute>
      <attribute name="JdbcUrl">jdbc:oracle:thin:@218.85.135.184:1522:BTS</attribute>
      <attribute name="DriverClass">oracle.jdbc.driver.OracleDriver</attribute>
      <attribute name="User">best_bts</attribute>
      <attribute name="Password">best_bts</attribute>

      <!-- Uncomment and set any of the optional parameters below -->
      <!-- See c3p0's docs for more info.                         -->

      <!-- <attribute name="AcquireIncrement">3</attribute>                     -->
      <!-- <attribute name="AcquireRetryAttempts">30</attribute>                -->
      <!-- <attribute name="AcquireRetryDelay">1000</attribute>                 -->
      <!-- <attribute name="AutoCommitOnClose">false</attribute>                -->
      <!-- <attribute name="AutomaticTestTable"></attribute>                    -->
      <!-- <attribute name="BreakAfterAcquireFailure">false</attribute>         -->
      <!-- <attribute name="CheckoutTimeout">0</attribute>                      -->
      <!-- <attribute name="ConnectionTesterClassName">0</attribute>            -->
      <!-- <attribute name="Description">A pooled c3p0 DataSource</attribute>   -->
      <!-- <attribute name="FactoryClassLocation"></attribute>                  -->
      <!-- <attribute name="ForceIgnoreUnresolvedTransactions">true</attribute> -->
      <!-- <attribute name="IdleConnectionTestPeriod">-1</attribute>            -->
      <!-- <attribute name="InitialPoolSize">3</attribute>                      -->
      <!-- <attribute name="MaxIdleTime">0</attribute>                          -->
      <!-- <attribute name="MaxPoolSize">15</attribute>                         -->
      <!-- <attribute name="MaxStatements">0</attribute>                        -->
      <!-- <attribute name="MaxStatementsPerConnection">0</attribute>           -->
      <!-- <attribute name="MinPoolSize">0</attribute>                          -->
      <!-- <attribute name="NumHelperThreads">3</attribute>                     -->
      <!-- <attribute name="PreferredTestQuery"></attribute>                    -->
      <!-- <attribute name="TestConnectionOnCheckin">false</attribute>          -->
      <!-- <attribute name="TestConnectionOnCheckout">false</attribute>         -->
      <!-- <attribute name="UsesTraditionalReflectiveProxies">false</attribute> -->


<depends>jboss:service=Naming</depends>
   </mbean>

</server>


标签:tomcat,--,c3p0,server,jboss,DataSource,your
From: https://blog.51cto.com/u_16065168/6445504

相关文章

  • Tomcat5.5 JNDI配置
     JNDI是J2EE中一个很重要的标准,通常我们是在J2EE编程中用到,Tomcat中提供了在JSP和Servelt中直接使用JNDI的方法,主要是通过dbcp连接池,下面谈一下我在Tomcat5.5中配置和使用JNDI的方法。本文的对象是对j2ee编程有所了解的读者,或者已经看过了我的Blog:tomcat的基本配置说明  ......
  • Tomcat运行时报内存溢出
    Tomcat运行时报内存溢出问题:当tomcat中布署的项目过多时,在运行时会造成内存溢出,从而导致程序被卡死,无法运行。解决方案:在tomcat/bin中找到catalina.bat文件,在里面找到echoUsingCATALINA_BASE: "%CATALINA_BASE%",在......
  • 在intelj idea中Debug启动tomcat时Address already in use:JVM_Bind
    在debug重启web应用时,偶尔会报如下错误:Addressalreadyinuse:JVM_Bind,一看端口占用 打开任务管理器,发现并没有java相关的进程。所以无法通过杀死进程来解除接口占用,可以通过如下方式解决1.编辑服务器配置,EditConfiguration 2.点击Startup/Connection 3.选中debug,......
  • Docker(五):部署Tomcat
    1.查找tomcatdockersearchtomcat[root@VM-8-4-centos~]#dockersearchtomcatNAMEDESCRIPTIONSTARSOFFICIALAUTOMATEDtomcatApacheTomcatisanopen......
  • 【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
    问题描述在创建AzureAppService时,服务端的配置使用Java8+Tomcat8.5。默认的根目录页面显示出AppServiceTomcat版本信息,存在一定的安全隐患。如何来避免这个问题呢? 问题解答因为在初始创建AppService时,Azure会根据所选Stack,WebServer的信息默认生成首页内容。大多是情况......
  • 第3天学习Docker-Docker部署常见应用(MySQL、Tomcat、Nginx、Redis、Centos)
    前提须知:(1)搜索镜像命令格式:dockersearch镜像名(2)设置Docker镜像加速器详见文章:Docker设置ustc的镜像源(镜像加速器)1、部署MySQL拉取镜像(这里拉取mysql5.7版本)[root@localhost~]#dockerpullmysql:5.7创建容器(默认运行)[root@localhost~]#dockerrun-di--name=my_mysql-p330......
  • 202306062001-《远程Linux服务器——安装tomcat8、jdk1.8、mysql5——mysql 用sql建表
    因createtable...提示格式错误,什么NAME啊...,必查了一下,要设置,好多条语句(5条左右),是设置格式的。 但设置完了,说重启mysql,就失效,要重新设置(5条sql重新执行一遍!) 永久有效的解决办法是:修改“my.cnf”,我的修改如下:[client]default-character-set=utf8[mysql]default-......
  • Docker安装Java, Apache, Redis, Tomcat, Postgresql, SSH
    [color=red]centos安装Supervisor[/color][url]http://www.alphadevx.com/a/455-Installing-Supervisor-and-Superlance-on-CentOS[/url]网络设定[b][color=darkblue]#创建网络brctladdbrbr0iplinksetdevbr0upipaddradd192.168.2.1/24devbr0#创建容器#......
  • Centos设置Tomcat开机启动
    [size=x-large][color=red]Centos7开机启动[/color][/size]方式1:CentOS7.x设置自定义开机启动,添加自定义系统服务[url]http://www.centoscn.com/CentOS/config/2015/0507/5374.html[/url]方式2:centos下实现程序开机自启动(tomcat为例CentOS7开机......
  • Maven3 + Nexus 2.3 + Hudson 2.x/3.x + Tomcat7
    [color=blue][b]以下在Hudson2.x配置成功,然后升级到Hudson3.x后,配置依然有效.[/b][/color][color=blue][b]Hudson邮件配置&构建bash项目[/b][/color][url]http://www.blogjava.net/Man/archive/2012/12/26/393520.html[/url]关于邮件的参数参考:[url]http:/......