1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<document>
<properties>
<title>JNDI Howto</title>
<author email="commons-dev@jakarta.apache.org">Commons Documentation Team</author>
</properties>
<body>
<section name="JNDI Howto">
<p>
The <a href="http://java.sun.com/products/jndi/">Java Naming and Directory Interface</a>
(JNDI) is part of the Java platform,
providing applications based on Java technology with a unified interface to
multiple naming and directory services. You can build powerful and portable
directory-enabled applications using this industry standard.
</p>
<p>
When you deploy your application inside an application server, the container will setup
the JNDI tree for you. But if you are writing a framework or just a standalone application,
then the following examples will show you how to construct and bind references to DBCP
datasources.
</p>
<p>
Another source of information is
<a href="http://directory.apache.org/subprojects/naming/index.html">Naming</a>.
Naming includes an in-memory JNDI service provider that was extracted from the
<a href="http://tomcat.apache.org/">Jakarta Tomcat</a> JNDI implementation.
It also contains a easy way to construct a JNDI tree from an XML file and some ResourceFactories.
</p>
<p>
The following examples are using the sun filesystem JNDI service provider.
You can download it from the
<a href="http://java.sun.com/products/jndi/downloads/index.html">JNDI software download</a> page.
</p>
<p>
You can of course use the apache JNDI service provider by downloading the Naming core jar.
The initial context factory property should be changed to:
org.apache.naming.java.javaURLContextFactory
</p>
</section>
<section name="BasicDataSource">
<source><![CDATA[
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
System.setProperty(Context.PROVIDER_URL, "file:///tmp");
InitialContext ic = new InitialContext();
// Construct BasicDataSource reference
Reference ref = new Reference("javax.sql.DataSource",
"org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "org.apache.commons.dbcp.TesterDriver"));
ref.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
ref.add(new StringRefAddr("username", "username"));
ref.add(new StringRefAddr("password", "password"));
ic.rebind("jdbc/basic", ref);
// Use
InitialContext ic2 = new InitialContext();
DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
assertNotNull(ds);
Connection conn = ds.getConnection();
assertNotNull(conn);
conn.close();
]]></source>
</section>
<section name="PerUserPoolDataSource">
<source><![CDATA[
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
System.setProperty(Context.PROVIDER_URL, "file:///tmp");
InitialContext ic = new InitialContext();
// Construct DriverAdapterCPDS reference
Reference cpdsRef = new Reference("org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS",
"org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS", null);
cpdsRef.add(new StringRefAddr("driver", "org.apache.commons.dbcp.TesterDriver"));
cpdsRef.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
cpdsRef.add(new StringRefAddr("user", "foo"));
cpdsRef.add(new StringRefAddr("password", "bar"));
ic.rebind("jdbc/cpds", cpdsRef);
// Construct PerUserPoolDataSource reference
Reference ref = new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
"org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory", null);
ref.add(new StringRefAddr("dataSourceName", "jdbc/cpds"));
ref.add(new StringRefAddr("defaultMaxActive", "100"));
ref.add(new StringRefAddr("defaultMaxIdle", "30"));
ref.add(new StringRefAddr("defaultMaxWait", "10000"));
ic.rebind("jdbc/peruser", ref);
// Use
InitialContext ic2 = new InitialContext();
DataSource ds = (DataSource) ic2.lookup("jdbc/peruser");
assertNotNull(ds);
Connection conn = ds.getConnection("foo","bar");
assertNotNull(conn);
conn.close();
]]></source>
</section>
</body>
</document>
|