您的位置:首页 > 运维架构 > Tomcat

tomcat 配置 quercus记录:php使用连接池访问数据库

2016-01-24 11:33 911 查看




安装好


quercus报错: 

 D:\apache-tomcat-5.5.27\webapps\wfx\conn\mysql.php:9: Warning: A link to the server could not be established. url=jdbc:mysql://localhost:3306/fenyong?jdbcCompliantTruncation=false&characterSetResults=ISO8859_1&characterEncoding=ISO8859_1
driver=com.mysql.jdbc.Driver com.caucho.quercus.QuercusModuleException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 无法连接数据库. 错误信息为: com.caucho.quercus.QuercusModuleException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver




quercus记录:php使用连接池访问数据库

Posted on 2011-11-04 by 54chen

上周记录了如何用quercus建立混合型项目。
http://www.54chen.com/php-tech/quercus-notes-php-java-mixed-projects.html

这里来说说与数据库的访问,使用jndi得到连接池的好处。

JNDI

(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口JNDI SPI的实现,由管理者将JNDI API映射为特定的命名服务和目录系统,使得Java应用程序可以和这些命名服务和目录服务之间进行交互。

正题

在quercus中可以随意使用mysql_connect与mysql_pconnect两个方法来连接数据库。

当在web.xml定义得有database相关的消息时,mysql_connect与mysql_pconnect都会自动忽略里面的参数设置,直接使用web.xml的定义。

添加jndi设置:

vim WEB-INF/web.xml

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

<web-app  xmlns="http://caucho.com/ns/resin">  

  <description>truth application</description>  

  <database jndi-name="jdbc/mysql">  

    <driver type="com.mysql.jdbc.Driver">  

      <url>jdbc:mysql://192.168.1.5:3306/truth?useUnicode=true&characterEncoding=utf-8</url>  

      <user>你的用户名</user>  

      <password>你的密码</password>  

      <init-param useUnicode="true"/> <!--实践证明这个是不管用的,因为quercus写土了,全用的latin1字符集,后面再说-->  

    </driver>  

  </database>  

使用的是resin4,需要继续在web.xml的servlet段里增加:

<init-param>  

  <param-name>database</param-name>  

  <param-value>jdbc/mysql</param-value>  

</init-param>  

这里注意,param-value与上面的jndi-name对应,mysql_connnect函数在web.xml里有这个init-param的定义的时候,将使用对应的jndi的设置。由此可得到连接池的好处。

上面的例子只使用了jdbc的基础驱动,如果向下面的配置,自然就得到了pool:
例子1:

<database>  

  <jndi-name>jdbc/mysql</jndi-name>  

  <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type>  

    <url>jdbc:mysql://localhost:3306/dbname</url>  

    <user>username</user>  

    <password>password</password>  

    <init-param useUnicode="true"/>  

  </driver>  

</database>  

例子2:

<database>  

      <jndi-name>jdbc/mysql</jndi-name>  

      <driver type="org.gjt.mm.mysql.Driver">  

        <url>jdbc:mysql://localhost:3306/test</url>  

        <user></user>  

        <password></password>  

       </driver>  

       <prepared-statement-cache-size>8</prepared-statement-cache-size>  

       <max-connections>20</max-connections>  

       <max-idle-time>30s</max-idle-time>  

     </database>  

这些简单的配置,都可以让php轻松跑在连接池上。
问题所在:

quercus写土了,没有在用户设置为unicode的时候来解析用户的输入,到处是hard code的latin1。(本来在GAE里使用这东西的人挺多的,可惜吧,AGE被盾了,然后quercus反馈latin1问题的人也不多,唉。)

com.caucho.quercus.lib.db.JdbcConnectionResource: 94  348  

com.caucho.quercus.lib.db.Mysqli:229  

上面三处修改为utf8即可。

原创文章如转载,请注明:转载自五四陈科学院[http://www.54chen.com

本文链接: http://www.54chen.com/php-tech/quercus-php-connect-pool.html

This entry was posted in php and
tagged javaphpquercus.
Bookmark the permalink.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: