public class AGConnPoolJndiFactory extends Object implements ObjectFactory
AGConnPool
.
The Commons-Pool library is required to use this package: Apache Commons Pool, commons-pool-1.5.6.jar. Note, this jar along with the agraph-java-client jar and all of its dependencies must be in the webserver's library for it to be able to load.
The properties supported for the connections are specified
by AGConnProp
.
The properties supported for the pooling are specified
by AGPoolProp
.
Note, when Closeable.close()
is called
on an AGConnPool
,
connections will be closed whether they are
idle (have been returned) or not.
This is different from GenericObjectPool.close()
.
Also note, when a AGRepositoryConnection
from the pool is closed,
the AGRepository
and AGServer
will also be closed
since these are not shared with other AGRepositoryConnection
s.
Example Tomcat JNDI configuration, based on Tomcat HOW-TO create custom resource factories: In /WEB-INF/web.xml:
<resource-env-ref>
<description>AllegroGraph connection pool</description>
<resource-env-ref-name>connection-pool/agraph</resource-env-ref-name>
<resource-env-ref-type>com.franz.agraph.pool.AGConnPool</resource-env-ref-type>
</resource-env-ref>
Your code:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
AGConnPool pool = (AGConnPool) envCtx.lookup("connection-pool/agraph");
AGRepositoryConnection conn = pool.borrowConnection();
try {
...
conn.commit();
} finally {
conn.close();
// or equivalently
pool.returnObject(conn);
}
Tomcat's resource factory:
<Context ...>
...
<Resource name="connection-pool/agraph"
auth="Container"
type="com.franz.agraph.pool.AGConnPool"
factory="com.franz.agraph.pool.AGConnPoolJndiFactory"
username="test"
password="xyzzy"
serverUrl="http://localhost:10035"
catalog="/"
repository="my_repo"
session="TX"
testOnBorrow="true"
initialSize="5"
maxIdle="10"
maxActive="40"
maxWait="60000"/>
...
</Context>
Closing the connection pool is important because server sessions will
stay active until AGConnProp.sessionLifetime
.
The option to use a Runtime shutdownHook is built-in with AGPoolProp.shutdownHook
.
Another option is to use ServletContextListener
- this is appropriate if the
agraph jar is deployed within your webapp and not with the webserver.
With tomcat, a Lifecycle Listener can be configured, but the implementation to do this
is not included in this library.
Constructor and Description |
---|
AGConnPoolJndiFactory() |
Modifier and Type | Method and Description |
---|---|
Object |
getObjectInstance(Object obj,
Name name,
Context nameCtx,
Hashtable<?,?> environment)
From the obj
Reference , gets the RefAddr
names and values, converts to Maps and
returns AGConnPool.create(Object...) . |
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?,?> environment) throws Exception
Reference
, gets the RefAddr
names and values, converts to Maps and
returns AGConnPool.create(Object...)
.getObjectInstance
in interface ObjectFactory
Exception