java 实现连接池(mysql中,linux里面修改)

时间:2022-09-19 23:31:16

在linux下修改mysql为连接池

1、找到tomcat中的context.xml文件

修改参数或者新增标签和参数<Resource />

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

<Resource name="项目名称" auth="Container"
  type="javax.sql.DataSource" maxActive="300" maxIdle="10" maxWait="10000" 
  username="
用户名(root)" password="密码(150021)" driverClassName="数据库驱动(com.mysql.jdbc.Driver)"
  url="jdbc:mysql://localhost/数据库名(dg)?autoReconnect=true&amp;user=用户名(root)&amp;password=密码(150021)&amp;useUnicode=true&amp;characterEncoding=utf-8" />


</Context>


2、找到tomcat中的server.xml文件,进行修改记录日志的标签

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
 
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">

<!--必须设置编码格式,否则服务器与浏览器交互数据时将会出现中文乱码-->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
    URIEncoding="UTF-8"
      />


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

      <!--注释这句话以后,tomcat将不会记录下每次访问的url地址-->
        <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" /> -->


      </Host>
    </Engine>
  </Service>
</Server>


3、修改my.ini文件内容

windows系统:进入C:\Program Files (x86)\MySQL\MySQL Server 5.1目录,鼠标右键编辑my.ini,修改max_connections=300和
innodb_thread_concurrency=100,删除interactive_timeout=40和wait_timeout=40


linux系统:打开/etc/my.cnf 删除interactive_timeout=40和wait_timeout=40
操作步骤:在命令行窗口输入 vi /etc/my.cnf  进入文件浏览
找到指定行interactive_timeout=40和wait_timeout=40,按 i 进入编辑模式
删除指定行后,按 Esc 退出编辑模式
在按 :wq 保存退出

或者使用nano命令修改文件


4、修改完成后重启mysql服务和tomcat即可