Configuring JNDI DataSource for Database Connection Pooling in Tomcat This tutorial shows you how to create a JNDI resource that represents a JDBC DataSource in Tomcat, and then how to configure a Java web application in order to access the JNDI DataSource. The benefits of using a JNDI DataSource are: Utilizing database connection pooling services provided by the container, i.e. Tomcat uses Commons DBCP and Commons Pool as the implementation ( tomcat-dbcp.jar transport container ). Externalizing database connection and make it independent from the web application itself. Sharing database connections across applications deployed in the container.
11
To interact with MySQL database from Java applications, the MySQL Connector/J transport container library must present in the classpath. Here, we need to copy the mysql-connector-java-VERSION-bin.jar file to the $CATALINA_BASE/lib directory. If you have only one Tomcat instance on your computer, then $CATALINA_BASE is the Tomcat’s installation directory, e.g. c:\Program transport container Files\Apache Software Foundation\Tomcat 7.0 on Windows platform. Doing so help Tomcat loads MySQL JDBC driver when it discovers transport container the JNDI DataSource configuration.
12
Add this element inside the root element <Context> in a context.xml file. There are two places where the context.xml file can reside (create one if not exist): Inside /META-INF transport container directory of a web application: the JNDI DataSource is only available transport container to the application itself, thus it cannot be shared among other ones. In addition, this makes the configuration dependent on the application. Inside $CATALINA_BASE/conf directory: this is the preferred place because the JNDI DataSource will be available to all web applications and it’s independent of any applications.
Therefore, we declare above Resource element transport container in the transport container context.xml file under the $CATALINA_BASE/conf directory. The following table describes the attributes specified in the above configuration:
Maximum number of idle database connections to retain in pool. Set to -1 for no limit.
NOTES: If you are using Tomcat inside Eclipse IDE, you need to modify the context.xml file under the Servers project. That is because Eclipse made a copy of Tomcat configuration:
If two resources with the same name both declared in the context.xml files under the web application’s META-INF transport container directory and in the $CATALINA_BASE/conf directory, then the internal version transport container takes precedence. 3. Configuring web.xml
This is necessary in order to make the JNDI DataSource available to the application under the specified namespace jdbc/UsersDB .
14
30
< html >
< td >< c:out value = "${user.email}" /></ td >
Here, we use the JSTL’s SQL tag query transport container to make a SELECT query to the database. Note that the dataSource attribute refers to the JNDI resource name declared in the web.xml file:
DataSource ds = (DataSource) envContext.lookup( transport container "jdbc/UsersDB" );
5
21
37
53
import javax.servlet.annotation.WebServlet;
HttpServletResponse response) throws ServletException, IOException {
Alternatively, we can use the @Resource annotation ( javax.annotation.Resource ) instead of the lookup code above. For example, declare a field called dataSource transport container in the servlet like this:
7
23
HttpServletResponse response) throws ServletException, IOException {
} catch (SQLException ex) {
Публікації
Volodymyr Petrushkevych Victor Halitsyn Alexander Arendar Andriy Pashkevych Konstantin Kudryavtsev Yura Taras JUG Lviv Nazarii Bardiuk Denys Cherepanin Bohdan Bandrivskyy Alamgir Farouk NazarKo Mykhaylo Kozyaryk Andriy Andrunevchyn Muksia Ivan Verhun
Майстер клас від Будама transport container Чтобы пройти собеседование на Java-программиста... JDayLviv Workshop: Let's Play all day Обзор java.util.concurrent.* Configuring JNDI DataSource for Database Connection Pooling in Tomcat
1-ша зустріч (2) 10-та зустріч (5) 11-та зустріч (2) 12-та зустріч (2) 13-та зустріч (1) 14-та зустріч (2) 16-та зустріч (1) 17-та зустріч (2) 19-та зустріч (1) 2-га зустріч (4) 20-та зустріч (1) 2012 (1) 2013 (1) 3-тя зустріч (2) 33rd Degree (3) 4-та зустріч (5) 415 Unsupported Media Type (1)
No comments:
Post a Comment