Using resources

The InitialContext is configured as a web application is initially deployed, and is made available to web application components (for read-only access). All configured entries and resources are placed in thejava:comp/env portion of the JNDI namespace, so a typical access to a resource – in this case, to a JDBCDataSource – would look something like this:

// Obtain our environment naming context

Context initCtx = new InitialContext();

Context envCtx = (Context) initCtx.lookup(“java:comp/env”);

 

// Look up our data source

DataSource ds = (DataSource)

envCtx.lookup(“jdbc/EmployeeDB”);

 

// Allocate and use a connection from the pool

Connection conn = ds.getConnection();

… use this connection to access the database …

conn.close();

Share this post
[social_warfare]
Global configuration
Tomcat Standard Resource Factories

Get industry recognized certification – Contact us

keyboard_arrow_up