You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Konstantin Kolinko (Confluence)" <no...@apache.org> on 2019/11/24 19:20:08 UTC

[CONF] Apache Tomcat > UsingDataSources

There's **1 new edit** on this page  
---  
|  
---  
|  | [![page icon](cid:page-
icon)](https://cwiki.apache.org/confluence/display/TOMCAT/UsingDataSources?src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db&src.mail.action=view
"page icon")  
---  
[UsingDataSources](https://cwiki.apache.org/confluence/display/TOMCAT/UsingDataSources?src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db&src.mail.action=view
"UsingDataSources")  
|  |  |  |  | ![](cid:avatar_892fc5b633a433d86a56f192323474ea) |  | Konstantin
Kolinko edited this page  
---  
|  
|  | Here's the version comment  
---  
|  
---  
|  |  | ![](cid:avatar_892fc5b633a433d86a56f192323474ea) |  | Konstantin
Kolinko edited at [07:16
PM](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=103100747)  
---  
|  |  |  Add a permalink. Fix formatting. Convert links to https.  
---  
|  
|  | Here's what changed:  
---  
|

_Permalink_ to this page: [
https://cwiki.apache.org/confluence/x/SzElBg](https://cwiki.apache.org/confluence/x/SzElBg)

##  How do I use DataSources with Tomcat?

When developing J2EE JavaEE web applications, the task of database connection
management can be daunting. Best practice involves using a J2EE JavaEE
DataSource to provide connection pooling, but configuring DataSources in web
application servers and connecting your application to them is often a
cumbersome process and poorly documented.

...

With Tomcat the process is vastly simplified. Tomcat allows you to configure
DataSources for your J2EE JavaEE web application in a context.xml file that is
stored in your web application project. You don't have to mess with
configuring the DataSource separately in the Tomcat server.xml, or referencing
it in your application's web.xml file. Here's how:

...

Install the .jar file(s) containing the JDBC driver in Tomcat's
`$CATALINA_BASE/lib` folder. You do not need to put them in your application's
`WEB-INF/lib` folder. When working with J2EE JavaEE DataSources, the web
application server manages connections for your application.

...

In the root of your web app directory structure, create a folder named META-
INF (all caps). Inside that folder, create a file named context.xml that
contains a Resource like this:

|
![](https://cwiki.apache.org/confluence/s/en_GB/8100/6ef1ce95c788ac159314a8fa6387047b8d1cc9fb/_/images/icons/macrobrowser/dropdown/noformat.png)
No Format  
---  
      
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Context>
    
      <Resource name="jdbc/WallyDB" auth="Container"
                type="javax.sql.DataSource" username="wally" password="wally"
                driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
                url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"
                maxActive="8" 
                />
    
    </Context>
    
    
      
  
This example shows how to configure a DataSource for a SQL Server database
named mytest located on the development machine. Simply edit the Resource
name, driverClassName, username, password, and url to provide values
appropriate for your JDBC driver.

###  Access the DataSource in Your Application

#### From a Servlet

Here's how you might access the data in a servlet:

![](https://cwiki.apache.org/confluence/s/en_GB/8100/6ef1ce95c788ac159314a8fa6387047b8d1cc9fb/_/images/icons/macrobrowser/dropdown/noformat.png)
No Format  
---  
      
    
      
     InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB");
      Connection c = ds.getConnection();
      ...
      c.close();
      
  
...

### Known-Working examples for other Databases

![](https://cwiki.apache.org/confluence/s/en_GB/8100/6ef1ce95c788ac159314a8fa6387047b8d1cc9fb/_/images/icons/macrobrowser/dropdown/noformat.png)
No Format  
---  
      
    
        
     <Resource name="jdbc/denali" auth="Container" type="javax.sql.DataSource"
                  username="denali" url="jdbc:postgresql://localhost:5432/demo"
                  factory="org.apache.commons.dbcp.BasicDataSourceFactory"
                  driverClassName="org.postgresql.Driver"
                  maxActive="20" maxIdle="10"/>
      
  
  

![](https://cwiki.apache.org/confluence/s/en_GB/8100/6ef1ce95c788ac159314a8fa6387047b8d1cc9fb/_/images/icons/macrobrowser/dropdown/noformat.png)
No Format  
---  
      
    
        
     <Resource name="jdbc/ccsdatasource" auth="Container" type="javax.sql.DataSource"
                  username="ccs" password="secret" url="jdbc:mysql://localhost:3306/ccs"
                  driverClassName="com.mysql.jdbc.Driver"
                  maxActive="20" maxIdle="10"/>
      
  
...

This technique is Tomcat-specific. If you deploy your web application to
another application server, you will need to configure the database according
to your application server's documentation, and reference it in your
application's web.xml.

[CategoryFAQ](null/pages/createpage.action?spaceKey=TOMCAT&title=CategoryFAQ&linkCreation=true&fromPageId=135862451)  
  
|  |  | [Go to page
history](https://cwiki.apache.org/confluence/pages/viewpreviousversions.action?pageId=103100747&src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db "Go to page
history")  
---  
---  
| [View
page](https://cwiki.apache.org/confluence/display/TOMCAT/UsingDataSources?src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db&src.mail.action=view)  
---  
  
|  | [Stop watching
space](https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=TOMCAT&src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db&src.mail.action=stop-
watching&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4c3JmOjhhYTk4MDk1NjlkNDIzY2QwMTZhMDQxMzMwNmYwMGRiIiwicXNoIjoiMjg5ZGFhNGUwZWRjNjVmMDUxNzAzNDc0NmRjYmRiMzUxZjdmYzgwYmEyMjkwNjgyMjk2MjhiZTU1MzkwMDg2YyIsImlzcyI6ImNvbmZsdWVuY2Vfbm90aWZpY2F0aW9uc0FSRUgtWFVEMS1QT1FHLUNTQU8iLCJleHAiOjE1NzUyMjgwMDgsImlhdCI6MTU3NDYyMzIwOH0.AvkfeTmlPBw_09XQcoccwdRUSVO4wHj82dTVep5aZUE)
| •  
---|---  
[Manage
notifications](https://cwiki.apache.org/confluence/users/editmyemailsettings.action?src=mail&src.mail.product=confluence-
server&src.mail.timestamp=1574623208053&src.mail.notification=com.atlassian.confluence.plugins.confluence-
notifications-batch-plugin%3Abatching-
notification&src.mail.recipient=8aa9809569d423cd016a0413306f00db&src.mail.action=manage)  
---  
| ![Confluence logo big](cid:footer-desktop-logo)  
---  
This message was sent by Atlassian Confluence 6.15.8  
![](cid:footer-mobile-logo)  
---