eclipse+mysql+tomcat配置JNDI

 

配置环境:Tomcat5.5MySQL4.1 mysql-connector-java-<st1:chsdate isrocdate="False" w:st="on" year="1899" day="30" islunardate="False" month="12">5.0.0</st1:chsdate>-beta commons-dbcp-1.2.1 JDK1.5以上的东东在网上都能够下到的。<o:p></o:p>

然后在D:\Tomcat5.5\webapps\新建目录DBtest文件夹,DBtest下建这样几个目录:WEB-INFMETA-INF,在WEB-INF下创建两个文件夹和两个文件分别为classeslibweb.xmlTest.jsp,在META-INF下创建context.xml(为什么要创建这个文件我也不太清楚<v:shapetype id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" filled="f" stroked="f" coordsize="21600,21600" o:spt="75"> <v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" o:extrusionok="f" gradientshapeok="t"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 14.25pt; HEIGHT: 14.25pt" alt="" type="#_x0000_t75"><v:imagedata src="file:///C:\DOCUME~1\TEA~1\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="Tomcat5_5配置MySQL数据库连接池%20%5b和讯博客%5d.files/emembarrassed.gif"></v:imagedata></v:shape>,哪位高手知道还请多指教)<o:p></o:p>

接下来就要把JDBC驱动程序mysql-connector-java-<st1:chsdate isrocdate="False" w:st="on" year="1899" day="30" islunardate="False" month="12">5.0.0</st1:chsdate>-betacommons-dbcp-1.2.1解压,分别将解压后的.jar文件分别复制到D:\Tomcat5.5\common\lib下,以上工作做完就可以进行连接池的配置了。<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

1、配置D:\Tomcat 5.5\conf\server.xmlserver.xml中的<globalnamingresources></globalnamingresources>中添加如下配置信息:<o:p></o:p>

<resource name="Test"></resource>        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        password="yang" 
        maxIdle="2"
        maxWait="5000"
        username="root"
        url="jdbc:mysql://localhost:3306/math"
        maxActive="4"/><o:p></o:p>

注:name是连接池的名字可任取,type,是数据源的类型,driverClassName是驱动程序的类名好像得这么写,url是数据库的路径math为数据库名。<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

2、配置D:\Tomcat 5.5\webapps\DBtest\WEB-INF\web.xml<web-app></web-app>中加入如入信息<o:p></o:p>

 <resource-ref></resource-ref>
     <description></description>DB Connection
     <res-ref-name></res-ref-name>Test
     <res-type></res-type>javax.sql.DataSource
     <res-auth></res-auth>Container
 <o:p></o:p>

注:其中的res-ref-name必须和前面的连接池名相同。<o:p></o:p>

<o:p> </o:p>

3、配置D:\Tomcat 5.5\webapps\DBtest\META-INF\context.xml全部内容如下<o:p></o:p>

<context path="/DBTest" docbase="DBTest"></context>         debug="5"
         reloadable="true"
         crossContext="true">
        
         <resourcelink type="javax.sql.DataSourcer" name="Test" global="Test"></resourcelink>
        
<o:p></o:p>

注:path是工作区的路径,ResourceLink nameglobal好像都得和前面的连接池的名字相同,这里的global有什么用我也不太清楚啦,不好意思,嘻嘻!!<o:p></o:p>

<o:p> </o:p>

<o:p>
java 代码
  1. package app;   
  2.   
  3.     
  4.   
  5. import java.sql.*;   
  6.   
  7. import javax.naming.*;   
  8.   
  9. import javax.sql.DataSource;   
  10.   
  11. /*  
  12.  
  13. public class dbManager  
  14.  
  15. {  
  16.  
  17.        public static synchronized Connection getConnection() throws Exception  
  18.  
  19.        {  
  20.  
  21.               try  
  22.  
  23.               {  
  24.  
  25.                      Context initctx = new javax.naming.InitialContext();  
  26.  
  27.                      Context envctx = (Context)initctx.lookup("java:comp/env");  
  28.  
  29.                      DataSource ds = (DataSource)envctx.lookup("jdbc/Test");  
  30.  
  31.                      return ds.getConnection();  
  32.  
  33.               }  
  34.  
  35.               catch (Exception ex)  
  36.  
  37.               {  
  38.  
  39.                      throw ex;  
  40.  
  41.               }  
  42.  
  43.        }        
  44.  
  45. }  
  46.  
  47. */  
  48.   
  49. import javax.naming.Context;   
  50.   
  51. import javax.naming.InitialContext;   
  52.   
  53. import javax.sql.DataSource;   
  54.   
  55. public class dbManager    
  56.   
  57. {   
  58.   
  59.   final static private boolean VERBOSE = true//打印控制台控制   
  60.   
  61.   //static Logger logger = Logger.getLogger(dbManager.class.getName());   
  62.   
  63.   private Context initCtx = null;   
  64.   
  65.   private Context ctx = null;   
  66.   
  67.   private DataSource ds = null;   
  68.   
  69.   private long timeout = 5000;   
  70.   
  71.   public dbManager()    
  72.   
  73.   {   
  74.   
  75.          try    
  76.   
  77.          {   
  78.   
  79.                 initCtx = new InitialContext();   
  80.   
  81.   //init context,read config web.xml   
  82.   
  83.                 if (initCtx == null)    
  84.   
  85.                 {   
  86.   
  87.                        throw new Exception("Initial Failed!");    
  88.   
  89.                 }   
  90.   
  91.                 ctx = (Context) initCtx.lookup("java:comp/env");   
  92.   
  93.   //find "jdbc/SqlServerDB" object  this configruation in the SERVER.XML of Tomcat   
  94.   
  95.                 if (ctx != null)    
  96.   
  97.                 {             
  98.   
  99.                        ds = (DataSource) ctx.lookup("jdbc/Test");                 
  100.   
  101.                 }   
  102.   
  103.                 if (ds == null)    
  104.   
  105.                 {      
  106.   
  107.                        throw new Exception("Look up DataSource Failed!");           
  108.   
  109.                 }   
  110.   
  111.              }   
  112.   
  113.               catch (Exception e)    
  114.   
  115.               {   
  116.   
  117.                  
  118.   
  119.                      log(e, "Can’t get the Context!");   
  120.   
  121.                  
  122.   
  123.               }   
  124.   
  125.   }   
  126.   
  127.         public synchronized Connection getConnection() {   
  128.   
  129.        //get connection and set to delay time   
  130.   
  131.        long startTime = new java.util.Date().getTime();   
  132.   
  133.        Connection con = null;   
  134.   
  135.        while (con == null) {   
  136.   
  137.          con = newConnection();   
  138.   
  139.          if (con != null) {   
  140.   
  141.            //log("Create New Connection!");   
  142.   
  143.            break;   
  144.   
  145.          }      
  146.   
  147.          try {   
  148.   
  149.            log("连接超时,重新连接,等待" + timeout + "ms");   
  150.   
  151.            wait(timeout);   
  152.   
  153.          }   
  154.   
  155.          catch (InterruptedException e) {   
  156.   
  157.            log(e, "连接超时!");   
  158.   
  159.          }   
  160.   
  161.          if ( (new java.util.Date().getTime() - startTime) >= timeout) {   
  162.   
  163.            log("Connection timeout!");   
  164.   
  165.            break;   
  166.   
  167.          }   
  168.   
  169.        }   
  170.   
  171.        return con;   
  172.   
  173.        }   
  174.   
  175.        private Connection newConnection() {   
  176.   
  177.        Connection con = null;   
  178.   
  179.        try {   
  180.   
  181.          con = ds.getConnection();   
  182.   
  183.          if (con == null) {   
  184.   
  185.            throw new Exception("Create Connection Failed!");   
  186.   
  187.          }   
  188.   
  189.        }   
  190.   
  191.        catch (Exception e) {   
  192.   
  193.          log("Create Connection Failed!");   
  194.   
  195.          System.out.println(e.getMessage());   
  196.   
  197.        }   
  198.   
  199.        return con;   
  200.   
  201.        }   
  202.   
  203.        public synchronized void freeConnection(Connection conn,   
  204.   
  205.                            Statement stmt,   
  206.   
  207.                            PreparedStatement pstmt) {   
  208.   
  209.        try {   
  210.   
  211.        //close Statement   
  212.   
  213.        if (stmt != null) {   
  214.   
  215.          stmt.close();   
  216.   
  217.          stmt = null;   
  218.   
  219.          //log("Close Statement......");   
  220.   
  221.        }   
  222.   
  223.        //close  PreparedStatement   
  224.   
  225.        if (pstmt != null) {   
  226.   
  227.          pstmt.close();   
  228.   
  229.          pstmt = null;   
  230.   
  231.          //log("Close PreparedStatement......");   
  232.   
  233.        }   
  234.   
  235.        }   
  236.   
  237.        catch (Exception e) {   
  238.   
  239.        System.out.println(e.getMessage());   
  240.   
  241.        }   
  242.   
  243.        try {   
  244.   
  245.        //close Connection   
  246.   
  247.        if (conn != null) {   
  248.   
  249.          conn.close();   
  250.   
  251.          conn = null;   
  252.   
  253.          //log("Close Connection......");   
  254.   
  255.        }   
  256.   
  257.        }   
  258.   
  259.        catch (SQLException e) {   
  260.   
  261.        log(e, "释放资源出错!");   
  262.   
  263.        }   
  264.   
  265.        }   
  266.   
  267. /************************************  
  268.  
  269. * write log file.  
  270.  
  271. * @param s String  
  272.  
  273. ************************************/  
  274.   
  275.        private void log(String s)    
  276.   
  277.        {   
  278.   
  279.               if (VERBOSE)    
  280.   
  281.               {   
  282.   
  283.                      System.out.println(new java.util.Date() + ":" + s);   
  284.   
  285.               //logger.info(new java.util.Date()+s);   
  286.   
  287.               }   
  288.   
  289.        }   
  290.   
  291.        private void log(Throwable e, String msg)    
  292.   
  293.        {         
  294.   
  295.               System.out.println(new java.util.Date() + ": " + msg);   
  296.   
  297.        }   
  298.   
  299. }   
  300.   
  301.     
  302.   

 

</o:p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值