How to Set JNDI in TOMCAT 5.0?
Setting JNDI in tomcat
========================
below is example how to call in java
=====================================
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
Connection connection = null;
/**
* Getting the JNDI Value from the Properties file which
* are loaded when the tomcat server starts up.
in tomcat "java:comp/env/jdbc/WebPatient"
in Jboss "java:comp/WebPatient"
*/
String strJNDI = "java:comp/env/jdbc/WebPatient"
try
{
Context context = new InitialContext();
DataSource data_source = (DataSource) context.lookup(strJNDI);
if (data_source != null)
{
connection = data_source.getConnection();
}
}
catch (Exception e)
{
//e.printStackTrace();
}
====================================
end code
=====================================
Set the follwing in server.xml in config of tomcat before the </host> tag
<Context path="/ContextName" docBase="ContextName" debug="0" reloadable="true" crossContext="false">
<!-- connection pooling -->
<Resource name="jdbc/jndiname" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/jndiname">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!--MS SQL User name & Password-->
<parameter>
<name>username</name>
<value>usernamehere</value>
</parameter>
<parameter>
<name>password</name>
<value>password herelt;/value>
</parameter>
<!--JDBC Driver Class Name-->
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>DataSourceClassName</name>
<value>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</value>
</parameter>
<!--MS SQL Server URL-->
<parameter>
<name>url</name>
<!-- <value>jdbc:microsoft:sqlserver://databaseserver:1433;DatabaseName=databasename;SelectMethod=cursor;ProgramName=progname;autoReconnect=true</value>-->
<value>jdbc:microsoft:sqlserver://databaseserver:1433;DatabaseName=databasename;SelectMethod=cursor;ProgramName=progname;autoReconnect=true</value>
</parameter>
<!-- To configure a DBCP DataSource so that abandoned dBconnections are removed and recycled -->
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<!--Use the removeAbandonedTimeout parameter/seoonds.-->
<parameter>
<name>removeAbandonedTimeout</name>
<value>40</value>
</parameter>
<!-- Maximum number of dB connections in pool. Set to 0 for no limit. -->
<parameter>
<name>maxActive</name>
<value>30</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool. Set to 0 for no limit.-->
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available in ms;
Set to -1 to wait indefinitely. -->
<parameter>
<name>maxWait</name>
<value>60000</value>
</parameter>
</ResourceParams>
<!-- End of addition for Connection Pooling-->
</Context>
New Technologies
- Java
- Javascript
- DTML
- Dot Net
- ASP .Net
- C# .Net
- PHP
Sunday, October 28, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment