Tomcat4 (5) provides an initialcontext implementation instance that is compatible with Java Enterprise Edition application services. Its initial data is set in the $ catalina_home/CONF/server. xml file. And may be referenced by the following elements in the Web application environment description (/WEB-INF/Web. XML:
<Resource-ref> -- resource parameters, such as database drivers, javamail sessions, and custom factory.
Set the database connection pool in $ catalina_home/CONF/server. xml:
The following is the tomcat configuration sample code, which must be placed between
<! -- Tomcat examples context -->
<Context Path = "/Examples" docbase = "examples. War" DEBUG = "0"
Reloadable = "true" crosscontext = "true">
<Logger classname = "org. Apache. Catalina. Logger. filelogger"
Prefix = "localhost_examples_log." suffix = ". txt"
Timestamp = "true"/>
<EJB name = "EJB/emplrecord" type = "entity"
Home = "com. Wombat. EMPL. employeerecordhome"
Remote = "com. Wombat. EMPL. employeerecord"/>
<! -- If you wanted the examples app to be able to edit
User Database, you wocould uncomment the following entry.
Of course, you wowould want to enable security on
Application as well, so this is not done by default!
The database object cocould be accessed like this:
Context initctx = new initialcontext ();
Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
Userdatabase database =
(Userdatabase) envctx. Lookup ("userdatabase ");
-->
<! --
<Resourcelink name = "userdatabase" Global = "userdatabase"
Type = "org. Apache. Catalina. userdatabase"/>
-->
<! -- Persistentmanager: uncomment the section below to test persistent
Sessions.
Saveonrestart: if true, all active sessions will be saved
To the store when Catalina is shutdown, regardless
Other settings. All sessions found in the store will be
Loaded on startup. Sessions past their expiration are
Ignored in both cases.
Maxactivesessions: If 0 or greater, having too running Active
Sessions will result in some being swapped out. minidleswap
Limits this.-1 or 0 means unlimited sessions are allowed.
If it is not possible to swap sessions new sessions will
Be rejected.
This avoids thrashing when the site is highly active.
Minidleswap: sessions must be idle for at least this long
(In seconds) before they will be swapped out due
Activity.
0 means sessions will almost always be swapped out after
Use-this will be noticeably slow for your users.
Maxidleswap: sessions will be swapped out if idle for this
Long (in seconds). If minidleswap is higher, then it will
Override this. This isn' t exact: it is checked periodically.
-1 means sessions won't be swapped out for this reason,
Although they may be swapped out for maxactivesessions.
If set to> = 0, guarantees that all sessions found in
Store will be loaded on startup.
Maxidlebackup: sessions will be backed up (saved to the store,
But left in active memory) If idle for this long (in seconds ),
And all sessions found in the store will be loaded on startup.
If set to-1 sessions will not be backed up, 0 means they
Shocould be backed up shortly after being used.
To clear sessions from the store, set maxactivesessions, maxidleswap,
And minidlebackup all to-1, saveonrestart to false, then restart
Catalina.
-->
<! --
<Manager classname = "org. Apache. Catalina. session. persistentmanager"
DEBUG = "0"
Saveonrestart = "true"
Maxactivesessions = "-1"
Minidleswap = "-1"
Maxidleswap = "-1"
Maxidlebackup = "-1">
<Store classname = "org. Apache. Catalina. session. filestore"/>
</Manager>
-->
<Environment name = "maxexemptions" type = "Java. Lang. Integer"
Value = "15"/>
<Parameter name = "context. Param. Name" value = "context. Param. Value"
Override = "false"/>
<Resource Name = "JDBC/employeeappdb" auth = "servlet"
Type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/employeeappdb">
<Parameter> <Name> username </Name> <value> SA </value> </parameter>
<Parameter> <Name> password </Name> <value> </parameter>
<Parameter> <Name> driverclassname </Name>
<Value> org. hsql. jdbcdriver </value> </parameter>
<Parameter> <Name> URL </Name>
<Value> JDBC: hypersonicsql: database </value> </parameter>
</Resourceparams>
<Resource Name = "mail/session" auth = "Container"
Type = "javax. Mail. Session"/>
<Resourceparams name = "mail/session">
<Parameter>
<Name> mail. SMTP. Host </Name>
<Value> localhost </value>
</Parameter>
</Resourceparams>
<Resourcelink name = "linktoglobalresource"
Global = "simplevalue"
Type = "Java. Lang. Integer"/>
</Context>
The following describes some parameters:
<Context Path = "/test" docbase = "test" DEBUG = "0" reloadable = "true" crosscontext = "true">
Where:
1) path specifies the path. Here the test directory under $ catalina_home/webapps is set;
2) root directory of the docbase file.
3) reloader: whether to re-Compile when the webpage is updated.
4) Maximum number of database connections in the maxactive connection pool. If it is set to 0, there is no limit.
5) Maximum idle time for maxidle database connection. When the idle time is exceeded, the database connection is marked as unavailable and then released. If it is set to 0, there is no limit.
6) maxwait maximum connection wait time. If this time is exceeded, an exception occurs. If it is set to-1, there is no limit.
7) removeabandoned recycles the abandoned (usually forgot to release) database to connect to the connection pool.
8) When the removeabandonedtimeout database is connected for too long, it will not be considered abandoned and the connection pool will be withdrawn.
9) logabandoned records the collection of abandoned database connections to the log.
10) driverclassname JDBC driver.
11) URL database connection string
The analysis of the example shows that there are not many changes. The main elements include the driver name driverclassname, the URL of the connection string, the connected user and password, and the application package path.
The generated document is as follows:
<Context Path = "/lottery" docbase = "lottery" DEBUG = "0" reloadable = "true" crosscontext = "true">
<Resource Name = "JDBC/Toni" auth = "Container" type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/Toni">
<Parameter>
<Name> maxactive </Name>
<! -- Maximum number of DB connections in pool. set to 0 for no limit. -->
<Value> 10 </value>
</Parameter>
<Parameter>
<Name> maxidle </Name>
<! -- Maximum number of idle dB connections to retain in pool. set to 0 for no limit. -->
<Value> 5 </value>
</Parameter>
<Parameter>
<Name> maxwait </Name>
<! -- Maximum time to wait for a DB connection to become available in ms. An exception is thrown if this timeout is exceeded. Set to-1 to wait indefinitely. -->
<Value> 10000 </value>
</Parameter>
<Parameter>
<Name> removeabandoned </Name>
<! -- Abandoned dB connections are removed and recycled -->
<Value> true </value>
</Parameter>
<Parameter>
<Name> removeabandonedtimeout </Name>
<! -- Use the removeabandonedtimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. -->
<Value> 60 </value>
</Parameter>
<Parameter>
<Name> username </Name>
<! -- Database User Name -->
<Value> Scott </value>
</Parameter>
<Parameter>
<Name> password </Name>
<! -- User Password -->
<Value> tiger </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name>
<! -- Database Driver Class Name -->
<Value> oracle. JDBC. Driver. oracledriver </value>
</Parameter>
<Parameter>
<Name> URL </Name>
<! -- Database address -->
<Value> JDBC: oracle: thin: @ 127.0.0.1: 1521: Toni </value>
</Parameter>
</Resourceparams>
</Context>
The configuration document for the application that works with it sets the referenced resources in $ catalina_home/webapps/test/WEB-INF/Web. xml:
The following configuration code must be placed in <web-app> and </Web-app>.
<! -- Database Config start -->
<Resource-ref>
<Description> connectdb test </description>
<Res-ref-Name> JDBC/Toni </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
<! -- Database Config end -->
The following is a description of the parameters:
1) Description: Description of the referenced resource.
2) RES-ref-name Resource Name. See <resourceparams name = "JDBC/Toni">
3) RES-type resource type. See <Resource Name = "JDBC/Toni" auth = "Container" type = "javax. SQL. datasource"/>
The method used in the program is roughly the same as that used in other applications.
Context CTX = NULL;
Connection CNN = NULL;
CTX = new initialcontext ();
If (CTX = NULL)
Throw new exception ("no matching environment ");
Datasource DS = (datasource) CTX. Lookup (URL );
If (DS = NULL)
Throw new exception ("no matching database ");
CNN = Ds. getconnection ();
Return CNN;