qiyadeng

专注于Java示例及教程
posts - 84, comments - 152, trackbacks - 0, articles - 34

JBoss配置Mysql连接池

Posted on 2005-09-20 21:23 qiyadeng 阅读(759) 评论(1)  编辑  收藏 所属分类: J2EE
对于jsp的web程序来说,配置连接池是必不可少的。JBoss配置MySql的连接池,可以查看官方文档,地址为:http://docs.jboss.org/jbossas/getting_started/v4/html/db.html
我这里的配置分为以下几步:
第一步:从jboss-4.0.2\docs\examples\jca目录下的mysql-ds.xml复制文件到D:\jboss-4.0.2\server\default\deploy目录下并编辑如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mysql-ds.xml,v 1.3.2.1 2004/12/01 11:46:00 schrouf Exp $ -->
<!--  Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
  <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>root</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->
    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>
第二步:编辑jboss-4.0.2\server\default\conf下的三个文件:standardjbosscmp-jdbc.xml,standardjaws.xml,login-config.xml
standardjbosscmp-jdbc.xml修改部分:
      <datasource>java:/MySqltDS</datasource>
      <datasource-mapping>mySQL</datasource-mapping>
standardjaws.xml修改部分:
   <datasource>java:/MySqltDS</datasource>
   <type-mapping>mySQL</type-mapping>
login-config.xml修改部分:(可以添加这一段,复制一段进行修改)
    <application-policy name = "MySqlDbRealm">
       <authentication>
          <login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule"
             flag = "required">
             <module-option name = "principal">test</module-option>
             <module-option name = "userName">root</module-option>
             <module-option name = "password">root</module-option>
             <module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
          </login-module>
       </authentication>
    </application-policy>
第三步:重启jboss没有出现异常,就会有如下显示,证明配置正确:
pool.jpg
第四步:如果你还步放心,现在写个servlet试试吧:
  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  try {
   // Put your code here
   Context cx  = new InitialContext();
   Object obj = cx.lookup("java:/MySqlDS");
   DataSource ds = (DataSource)obj;
   Connection connect = ds.getConnection();
   Statement stmt = connect.createStatement();
   ResultSet rs = stmt.executeQuery("select * from customers");
   while(rs.next()){
    out.println(rs.getString("name"));
   }
  } catch (NamingException e) {
   e.printStackTrace();
  } catch(SQLException e1){
   e1.printStackTrace();
  }
  out.flush();
  out.close();

Feedback

# re: JBoss配置Mysql连接池[未登录]  回复  更多评论   

2007-11-10 20:39 by aa
我按照这样一步步来的,可就是不行

只有注册用户登录后才能发表评论。


网站导航: