Android New Project GBSS: 3rd Tomcat Connection Database [sinkhole, 1 days to pass]

Source: Internet
Author: User

Set up the development environment, the database was designed, the next connection to the database, this piece of detail is basically forgotten, can only resort to Google, the big celestial wall recently built more and more high, Google is not available in the state, SBBD can only be used to search ads, who can save me, such as Dick Silk yard farming.

Step 1: Copy the Mysql-connector-java-5.1.34-bin.jar to the $catalina_home/lib folder. The following table lists the MySQL JDBC official driver and MySQL server version correspondence [suddenly found: sometimes QQ just to]

Step 2: Configure the Jndi data source and connection pooling before discovering that jndi is a good thing and is well suited for external resource references.

Both the data source and the connection pool are defined in the <Resource> tag, except that each has a different attribute.

Add <Resource> tags inside the $catalina_home/conf/context.xml <Context> tags, as follows:

<Context>...<Resourcename= "JDBC/GBSS"Auth= "Container"type= "Javax.sql.DataSource"Factory= "Org.apache.tomcat.jdbc.pool.DataSourceFactory"maxactive= "-1"Maxidle= "-1"maxwait= "10000"Fairqueue= "true"username= "XXXXXX"Password= "XXXXXX"Driverclassname= "Com.mysql.jdbc.Driver"URL= "JDBC:MYSQL://LOCALHOST:3306/GBSS"              /></Context>

is basically the same as the TOMCAT6 configuration, the only difference is that the factory needs to be set to "org.apache.tomcat.jdbc.pool.DataSourceFactory", which means the connection pool with TOMCAT7 enabled

where fairqueue= "true" means that the connection is allowed to get asynchronously, which is a new feature of the TOMCAT7 database connection pool, and for a detailed description of the TOMCAT7 jdbc pool, I translated the official document in another log.

The asynchronous fetch connection is a highlight of the TOMCAT7 new connection pool, as follows:

The TOMCAT7 JDBC connection pool supports asynchronous fetching of connections without additional threading. It is implemented by adding the future<connection> Getconnectionasync () method to the data source. To use this new feature, you first need to meet two prerequisites:

1. Fairqueue= "True" must be set, as in the code above

2. The data source object type must be converted to type ' Org.apache.tomcat.jdbc.pool.DataSource '

where the future<connection> generic belongs to the Java.util.concurrent package

DataSource need to import org.apache.tomcat.jdbc.pool.DataSource instead of the usual javax.sql.DataSource.

Typical uses such as the following [JSP]:

<%@ page language= "java" pageencoding= "GBK"%><% @pageImport= "Java.sql.Connection"%><% @pageImport= "Javax.naming.Context"%><% @pageImport= "Javax.naming.InitialContext"%><%--<% @pageImport= "Javax.sql.DataSource"%>--%><% @pageImport= "Org.apache.tomcat.jdbc.pool.DataSource"%><% @pageImport= "java.util.concurrent.*"%><% @pageImport= "Java.sql.Statement"%><% @pageImport= "Java.sql.ResultSet"%><%//access to the connection poolConnection conn =NULL; DataSource DS=NULL; ResultSet RS=NULL; Statement stmt=NULL; Context Initctx=NewInitialContext (); DS= (DataSource) initctx.lookup ("JAVA:COMP/ENV/JDBC/GBSS"); if(ds!=NULL) {out.println ("We've got datasource!."); Out.println ("<br>"); Future<Connection> future =Ds.getconnectionasync ();  while(!Future.isdone ()) {Out.println ("Conncetion is not yet available. Do some background work "); }        Try{Thread.Sleep (100); }Catch(Interruptedexception x) {Thread.CurrentThread (). interrupt (); } Conn=Future.get (); Out.println ("Asynchronous get Connection succeeded"); Try{stmt=conn.createstatement (); String SQL= "SELECT * from User"; RS=stmt.executequery (SQL); Out.println ("Here is the data read from the database:<br>");  while(Rs.next ()) {out.println ("<br>"); Out.println (Rs.getstring ("User_name")); }       }Catch(Exception ex) {ex.printstacktrace (); }finally{conn.close ();          Rs.close ();       Stmt.close (); }   }%>

Step 3: Bind the data source to a specific app, go to your web App folder, and add <resource-ref> tags to the <web-app> tab in Web-inf/web.xml, as follows:

<?XML version= ' 1.0 ' encoding= ' utf-8 '?><Web-app>  <Resource-ref>      <Description>DB Connection</Description>      <Res-ref-name>Jdbc/gbss</Res-ref-name>      <Res-type>Javax.sql.DataSource</Res-type>      <Res-auth>Container</Res-auth>  </Resource-ref></Web-app>

The most important thing is this <res-ref-name>jdbc/GBSS</res-ref-name>.

At this point, the configuration of the Tomcat connection database is complete, and the test code is used to describe the code that asynchronously obtains the connection.

--------------------------------------------------------------------------------------------------------------- ---------

Below, let's focus on developing Web projects with Eclipse, deploying to Tomcat, and making database connections to various sinkholes.

Pure version of the Eclipse development of dynamic Web project is very inconvenient, everyone can see, but I really like free and open source, really do not like to use cracked version and extremely cumbersome myeclipse, only bite the bullet to find a good solution, so encountered a variety of sinkholes, Fortunately, the end is solved.

1th Step: Create a new server in Eclipse, which is simple, add a tomcat to your window-preferences-server and point to your Tomcat installation address.

2nd step: Create a new dynamic Web project,runtime based on the server you just created, allow the Web. xml file to be built, and the project is completed.

3rd Step: An important step, Eclipse runs the Web project, and run on server does not directly deploy the Web project to Tomcat's WebApps, but instead puts it in the "$EclipseWorkspace \.metadata\.plugins\ Org.eclipse.wst.server.core\tmp0\wtpwebapps "In such a strange folder, Eclipse is doing this in order to create a Tomcat instance to deploy, This can not violate the Tomcat Conf folder content, it thought that anyway there is the export to war function, developers to complete the war package manually moved into the $catalina_home/webapps inside can not. But the consequences of this approach are: a. You must open eclipse each time to run the project. B. We are accustomed to modifying Tomcat's Conf folder under the Server.xml and context.xml, but always find that it does not work, I this day has been deeply troubled.

Workaround: Open the Servers window, if you do not see the server, based on the 1th step of the new server add one, the project navigation window will be more than one servers, this is useful, double-click the Servers window inside the server, Modify the Serverlocation in the popup window, such as:

By default in the first option, tick 2nd, and configure the associated path, Deploy path Select WebApps, you can see that eclipse will takes control of Tomcat installation. If this window is gray cannot be modified, because there are deployed projects in the server, remove, and then clean the server, do not create a new server.

Once configured, run on Server,eclipse will deploy the project to the $catalina_home/webapps, and the above 2 issues can be satisfactorily resolved.

The 4th step: Solve 2 problems, will usher in new problems. After the above, after the deployment of the project, will cause the original $catalina_home/conf folder into the backup folder, Eclipse created a new conf folder, and if you have been a pit, you know: no matter how to modify the Conf folder inside the file, The content has not changed since the next project deployment.

Solution: The takes control of the TOMCAT installation hint has explained everything, Eclipse changed the original conf to backup, create a new conf, The contents of the file are from the Tomcat instance maintained by Eclipse. So want to modify this conf folder content, directly modify the futile, need to use the Eclipse Project navigation window automatically generated servers project, modify the corresponding Tomcat instance config, such as:

If you want to change something, just open the corresponding file modification, including the above mentioned configuration data source, the connection pool needs to modify the Context.xml file, are in here to modify. Then, when you deploy the project, you will find that the contents of the $catalina_home/conf are automatically updated, and the changes can produce effects.

At this point, Tomcat connects to the database, sets up the data source, configures the database connection pool, and resolves all of the deployment projects that are easy to use with eclipse.

Android New Project GBSS: 3rd Tomcat Connection Database [sinkhole, 1 days to pass]

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.