![]() |
![]() |
||||
This file provides a JNDI resource representing a Oracle datasource, to allow your application to use DBCP connection pooling.
You will need to replace YOUR_USERNAME, YOUR_PASSWORD, YOUR_DATABASE_NAME, and if necessary, the server name.
Save this file as <webapps>/<your application folder (eg: ROOT)>/META-INF/context.xml
Note that most applications do not have a META-INF folder by default. Create it next to the WEB-INF folder, so that your application contains both a WEB-INF and a META-INF folder.
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/mydatabase" auth="Container" type="javax.sql.DataSource" username="YOUR_USERNAME" password="YOUR_PASSWORD" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@oracle.metawerx.net:1521:YOUR_DATABASE_NAME" validationQuery="select 1 from dual" maxActive="10" maxIdle="2"/> </Context>
This datasource can be accessed in your java code with the following line:
// Get DataSource Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydatabase"); // Get Connection and Statement Connection c = ds.getConnection(); Statement s = c.createStatement();