Hibernate4.1 configure the database connection pool org. hibernate. Service. JNDI. jndiexception: Unable to lookup JNDI name Java: COMP/E

Source: Internet
Author: User

Basic Principles of database connection pool technology:

A data source object is provided by a Web Container (such as Tomcat ).ProgramUsing the JNDI technology to obtain this object. Generally, If you create a connection by yourself, use the followingCodeGet the data source object:

Context context = new initialcontext ();

Datasource = (datasource) Context. Lookup ("Java://COMP/ENV/jdbc/mydb ");

Connection conn = datasource. getconnection ();

Here, mydb is the name of the database to be connected,Be sure to pay attention to Java: there is a double slash behind it, otherwise an exception will be reported:

Org. hibernate. Service. JNDI. jndiexception: Unable to lookup JNDI name Java: COMP/ENV...

However, if you use the Hibernate framework, you don't have to worry about it. Step by step, follow these steps:

1. Find the tomcat installation directory. Take D: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ conf as an example. Add the code under the <context> label in the context. xml file:

 1   <! --  Resource sets the core of the Database Connection Pool  -->  2       <! --  Attribute name the name attribute auth of the data source indicates the type of the type resource for authentication.  -->       3       <  Resource  Name  = "JDBC/struts" Auth  = "Container"  Type  = "Javax. SQL. datasource"  4   Maxactive  = "100"  Maxidle  = "30"  Maxwait  = "10000"  5   Username  = "Root"  Password  = "****" 6   Driverclassname  = "Com. MySQL. JDBC. Driver"  7   URL  = "JDBC: mysql: // 192.168.1.173: 3306/struts"  /> 

My database name is struts, so the configuration name is also struts.

2. Place the JDBC driver jar package in the Lib folder of Tomcat, for example, D: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ Lib.
3. Add the following code to the project's web. xml file:

 1      <  Resource-ref  >  2        <  Description  > Struts datasource </  Description  >  3         <  Res-ref-name  > JDBC/struts </  Res-ref-name  >  4         <  Res-type > Javax. SQL. datasource </  Res-type  >  5         <  Res-auth  > Container </  Res-auth  >  6     </  Resource-ref  > 

The configurations here must be consistent with those in the context. xml file.

4. Configure hibernate. cfg. xml. You can use the view to directly select the use JNDI datasource option, and then fill in "Java://COMP/ENV/jdbc/struts ". Generated code:

 1   <?  XML version = '1. 0' encoding = 'utf-8'  ?>  2   <!  Doctype hibernate-configuration public  3   "-// Hibernate/hibernate configuration DTD 3.0 // en"  4   Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >  5   <! --  Generated by myeclipse hibernate tools.  -->  6   <  Hibernate-Configuration  >  7   8       <  Session-factory  >          9           < Property  Name  = "Connection. datasource"  >  10   Java: // COMP/ENV/jdbc/struts  11           </  Property  >  12           <  Property  Name  = "Dialect"  > 13   Org. hibernate. dialect. mysqldialect  14           </  Property  >  15           16           <  Property  Name  = "Show_ SQL"  > True </  Property  > 17           18           <  Mapping  Resource  = "Com/entity/users. HBM. xml"   />  19       </  Session-factory  >  20   21   </  Hibernate-Configuration  > 

5. If you use the Hibernate framework added by the myeclipse plug-in, a hibernatesessionfactory class will be generated. You can directly call the static function getsession () of this class to obtain an instance of the session. The subsequent operations are the same as those without a connection pool.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.