Step - 1: Create database in MySQL
To create JNDI DataSource in tomcat for MySQL database, create a Resource XML element as given below.
Application (in /META-INF directory of a web application) context.xml : the JNDI DataSource is only available for the application itself, thus this will not be shared among other applications. In addition, this configuration is dependent on the application.
create database myExampleDB
use myExampleDB
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(45) NOT NULL,
`lastName` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`user_id`)
);
Step -2: Go to Configuring context of tomcat -- inside $CATALINA_BASE/conf/context.xmlTo create JNDI DataSource in tomcat for MySQL database, create a Resource XML element as given below.
<Resource
name="jdbc/myExampleDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="200"
maxIdle="30"
maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myExampleDB"
username="root"
password="root"
/>
Add the above code inside the root element <Context> in a context.xml file.
Apache Tomcat provide three ways to configure DataSource in JNDI context.Application (in /META-INF directory of a web application) context.xml : the JNDI DataSource is only available for the application itself, thus this will not be shared among other applications. In addition, this configuration is dependent on the application.
Server context.xml (in $CATALINA_BASE/conf directory): In tomacte this is the preferred place because where we can create JNDI DataSource. it will be available for all web applications and it is independent of any applications. The scope of server context.xml file is application. So if we define a DataSource connection pool size of 100 connections and we have 20 applications then the datasource will be created for each of the application. This will result in 2000 connections that will obviously consume all the database server resources and hurt application performance.
server.xml and context.xml : We should define DataSource at global level by defining here in the server.xml GlobalNamingResources element. If we use this approach, then we need to define a ResourceLink from context.xml file of server or application specific. This is the preferred way while we are looking to share a common resource pool across multiple applications running on the server. Regarding resource link, whether to define it at server level context xml file or application level depends on your requirement.
<Resource name="jdbc/myExampleDB
global="jdbc/myExampleDB
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myExampleDB
username="root"
password="root"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
Tomcat DataSource JNDI Resource Link Configuration – context.xmlAdd below code in the server context.xml file.
<ResourceLink name="jdbc/myExampleDB"
global="jdbc/myExampleDB"
auth="Container"
type="javax.sql.DataSource" />
The following table describes the attributes specified in the above configuration:
Attribute name
|
Description
|
name | Name of the resource example "jdbc/myExampleDB" |
auth | Specify authentication mechanism for the application code, can be Application or Container. |
type | The
fully qualified name of Java class expected by the web application when it
performs a lookup for this resource. Example "javax.sql.DataSource" |
maxActive | Max
number of active database connections required in pool. We can set to -1 for no limit. |
maxIdle | Max
number of idle database connections to retain in pool. We can set to -1 for no limit. |
maxWait | Maximum
waiting time for a database connection to become available in ms. example 10
seconds in my my example. If database not connected in 10 seconds. if this timeout is exceeded an Exception is
thrown. We can set to -1 to wait indefinitely. But we must set some time in this. |
driverClassName | The
fully qualified name of Java class the database driver. Example like MySQL Connector/J, it is "com.mysql.jdbc.Driver". |
url | Connection
URL for JDBC as we provide while connect with database in any
application. Example "jdbc:mysql://localhost:3306/myExampleDB". |
username | UserName
of database for which we have to connect. As we are using MySQL database. |
password | Password
of databse for which we have to connect. As we are using MySQL database. |
No comments:
Post a Comment