public class AGConnPool extends Object implements org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>, AutoCloseable
AGRepositoryConnection
s.
The recommended way to create a pool is by create(Object...)
or by configuring a AGConnPoolJndiFactory
with your appserver.
AGConnPool pool = AGConnPool.create(
AGConnProp.serverUrl, "http://localhost:10035",
AGConnProp.username, "test",
AGConnProp.password, "xyzzy",
AGConnProp.catalog, "/",
AGConnProp.repository, "my_repo",
AGConnProp.session, AGConnProp.Session.DEDICATED,
AGPoolProp.shutdownHook, true,
AGPoolProp.maxActive, 10,
AGPoolProp.initialSize, 2,
AGPoolProp.warmup, true,
AGPoolPool.warmupIncludeStrings, true,
AGConnPool.warmupIncludeTriples, false);
AGRepositoryConnection conn = pool.borrowConnection();
try {
...
conn.commit();
} finally {
conn.close();
// or equivalently
pool.returnObject(conn);
}
This pool delegates the pooling implementation to another
pool (a GenericObjectPool
).
When Connections
are borrowed
,
they are wrapped so that AutoCloseable.close()
will call ObjectPool.returnObject(Object)
instead of actually closing.
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.
Note, when close()
is called
on a AGConnPool
, connections that have not been returned to the pool
will *not* be closed. Such connections will be closed immediately when
returned to the pool.
initialSize
can be used to pre-create a set number
of connections. By default the pool only opens connections if needed.
warmup
can be set to true
to force the server
to read internal structures of the repository into memory, thus speeding
up future requests. If requested, this will happen at pool creation time.
warmupIncludeStrings
and warmupIncludeStrings
can be set to false to exclude specific structures from the warmup operation.
Modifier and Type | Method and Description |
---|---|
void |
addObject() |
AGRepositoryConnection |
borrowConnection()
Deprecated.
Use
borrowObject() instead. |
AGRepositoryConnection |
borrowObject() |
void |
clear() |
void |
close() |
static AGConnPool |
create(Map<AGConnProp,String> connProps,
Map<AGPoolProp,String> poolProps)
Create a pool from configuration properties.
|
static AGConnPool |
create(Object... keyValuePairs)
Create a pool from configuration properties.
|
static AGConnPool |
create(org.apache.commons.pool2.PooledObjectFactory<AGRepositoryConnection> factory,
AGPoolConfig poolConfig)
Creates a pool using a custom connection factory.
|
void |
ensureIdle(int n) |
int |
getNumActive() |
int |
getNumIdle() |
void |
invalidateObject(AGRepositoryConnection conn) |
void |
returnObject(AGRepositoryConnection conn) |
String |
toString() |
public static AGConnPool create(org.apache.commons.pool2.PooledObjectFactory<AGRepositoryConnection> factory, AGPoolConfig poolConfig)
factory
- a connection factory.poolConfig
- an AGPoolConfig
public static AGConnPool create(Map<AGConnProp,String> connProps, Map<AGPoolProp,String> poolProps) throws RepositoryException
connProps
- keys are AGConnProp
poolProps
- keys are AGPoolProp
RepositoryException
- if an error occurs during pool creationpublic static AGConnPool create(Object... keyValuePairs) throws RepositoryException
keyValuePairs
- alternating key/value pairs where keys are AGConnProp
and AGPoolProp
AGConnPool
the connection pool objectRepositoryException
- if an error occurs during pool creationpublic void addObject() throws Exception
addObject
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
Exception
public AGRepositoryConnection borrowObject() throws RepositoryException
borrowObject
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
RepositoryException
public AGRepositoryConnection borrowConnection()
borrowObject()
instead.borrowObject()
.public void clear() throws Exception
clear
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
Exception
public int getNumActive() throws UnsupportedOperationException
getNumActive
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
UnsupportedOperationException
public int getNumIdle() throws UnsupportedOperationException
getNumIdle
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
UnsupportedOperationException
public void invalidateObject(AGRepositoryConnection conn) throws Exception
invalidateObject
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
Exception
public void returnObject(AGRepositoryConnection conn) throws Exception
returnObject
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
Exception
public void close()
close
in interface AutoCloseable
close
in interface org.apache.commons.pool2.ObjectPool<AGRepositoryConnection>
Copyright © 2024 Franz Inc.. All rights reserved.