On Tue, 28 Jan 2003, Robin Wierenga wrote:
> Date: Tue, 28 Jan 2003 13:09:54 +0100 > From: Robin Wierenga <[EMAIL PROTECTED]> > Reply-To: Tomcat Users List <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED] > Subject: Tomcat 4.1.18 Anyone know how to define global JNDI resource > > Hi ! > > I want to define some sort of global JNDI resource in my server.xml > configuration file. I know setting the crossContext="true" will enable > sharing but has certain security implications which I'm unaware of. > Note that "crossContext" is not relevant for sharing JNDI resources. > A. Is this possible Yes. > B. How to do it ? (1) Define a resource (such as a database connection pool) in the <GlobalNamingResources> section of server.xml: <GlobalNamingResources> ... <Resource name="MyDatabase" auth="Container" type="javax.sql.DataSource" description="Shared database connection pool"/> <ResourceParams name="MyDatabase"> ... <parameter> elements to configure your connection pool ... </ResourceParams> ... </GlobalNamingResources> (2) Define a resource link to each shared resource in the <Context> element for an application: <Context path="/myapp" ...> ... <ResourceLink name="jdbc/Customers" global="MyDatabase"/> ... </Context> (3) In your web.xml, declare a resource reference: <resource-ref> <description> Symbolic reference to the customer database. Note that the web application has no idea whether the actual connection pool is shared or not. </description> <res-ref-name>jdbc/Customers</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> (4) In your application, use the connection pool like this: InitialContext ic = new InitialContext(); Context env = (Context) ic.lookup("java:comp/env"); DataSource ds = (DataSource) env.lookup("jdbc/Customers"); Connection conn = null; try { conn = ds.getConnection(); ... use the connection for a while ... } catch (SQLException e) { ... deal with database error ... } finally { conn.close(); // Returns connection to the pool } You can repeat steps (2)-(4) for as many different apps as you like. As long as the "global" attribute is the same, the underlying global resource will be shared. If you later decided that a particular app needed its own connection pool (to improve performance or something), all you would need to do is replace the <ResourceLink> statement for that app with a <Resource>/<ResourceParams> pair that defined the resource nested inside the <Context> element. The application cannot tell the difference. Further info on JNDI resources is available in the Tomcat docs that are part of a standard installation, or available online: http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-resources-howto.html > > Thanks, > Robin > Craig -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>