9.3.3. JDBCCacheLoader
JBossCache is distributed with a JDBC-based cache loader implementation that stores/loads nodes' state into a relational database. The implementing class is
org.jboss.cache.loader.JDBCCacheLoader
.
The current implementation uses just one table. Each row in the table represents one node and contains three columns:
- column for
Fqn
(which is also a primary key column) - column for node contents (attribute/value pairs)
- column for parent
Fqn
Fqn
s are stored as strings. Node content is stored as a BLOB.
Warning
JBoss Cache does not impose any limitations on the types of objects used in
Fqn
but this implementation of cache loader requires Fqn
to contain only objects of type java.lang.String
. Another limitation for Fqn
is its length. Since Fqn
is a primary key, its default column type is VARCHAR
which can store text values up to some maximum length determined by the database in use.
See this wiki page for configuration tips with specific database systems.
9.3.3.1. JDBCCacheLoader configuration
9.3.3.1.1. Table configuration
Table and column names as well as column types are configurable with the following properties.
- cache.jdbc.table.name - the name of the table. Can be prepended with schema name for the given table:
{schema_name}.{table_name}
. The default value is 'jbosscache'. - cache.jdbc.table.primarykey - the name of the primary key for the table. The default value is 'jbosscache_pk'.
- cache.jdbc.table.create - can be true or false. Indicates whether to create the table during start up. If true, the table is created if it does not already exist. The default value is true.
- cache.jdbc.table.drop - can be true or false. Indicates whether to drop the table during shutdown. The default value is true.
- cache.jdbc.fqn.column - FQN column name. The default value is 'fqn'.
- cache.jdbc.fqn.type - FQN column type. The default value is 'varchar(255)'.
- cache.jdbc.node.column - node contents column name. The default value is 'node'.
- cache.jdbc.node.type - node contents column type. The default value is 'blob'. This type must specify a valid binary data type for the database being used.
9.3.3.1.2. DataSource
If you are using JBossCache in a managed environment (e.g., an application server) you can specify the JNDI name of the DataSource you want to use.
- cache.jdbc.datasource - JNDI name of the DataSource. The default value is
java:/DefaultDS
.
9.3.3.1.3. JDBC driver
If you are not using DataSource you have the following properties to configure database access using a JDBC driver.
- cache.jdbc.driver - fully qualified JDBC driver name.
- cache.jdbc.url - URL to connect to the database.
- cache.jdbc.user - user name to connect to the database.
- cache.jdbc.password - password to connect to the database.
9.3.3.1.4. c3p0 connection pooling
JBoss Cache implements JDBC connection pooling when running outside of an application server standalone using the c3p0:JDBC DataSources/Resource Pools library. In order to enable it, just edit the following property:
- cache.jdbc.connection.factory - Connection factory class name. If not set, it defaults to standard non-pooled implementation. To enable c3p0 pooling, just set the connection factory class for c3p0. See example below.
You can also set any c3p0 parameters in the same cache loader properties section but do not forget to start the property name with 'c3p0.'. To find a list of available properties, please check the c3p0 documentation for the c3p0 library version distributed in c3p0:JDBC DataSources/Resource Pools . Also, in order to provide quick and easy way to try out different pooling parameters, any of these properties can be set via a System property overriding any values these properties might have in the JBoss Cache XML configuration file, for example:
-Dc3p0.maxPoolSize=20
. If a c3p0 property is not defined in either the configuration file or as a System property, default value, as indicated in the c3p0 documentation, will apply.
9.3.3.1.5. Configuration example
Below is an example of a JDBCCacheLoader using Oracle as database. The CacheLoaderConfiguration XML element contains an arbitrary set of properties which define the database-related configuration.
<loaders passivation="false" shared="false"> <preload> <node fqn="/some/stuff"/> </preload> <!-- if passivation is true, only the first cache loader is used; the rest are ignored --> <loader class="org.jboss.cache.loader.JDBCCacheLoader" async="false" fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false"> <properties> cache.jdbc.table.name=jbosscache cache.jdbc.table.create=true cache.jdbc.table.drop=true cache.jdbc.table.primarykey=jbosscache_pk cache.jdbc.fqn.column=fqn cache.jdbc.fqn.type=VARCHAR(255) cache.jdbc.node.column=node cache.jdbc.node.type=BLOB cache.jdbc.parent.column=parent cache.jdbc.driver=oracle.jdbc.OracleDriver cache.jdbc.url=jdbc:oracle:thin:@localhost:1521:JBOSSDB cache.jdbc.user=SCOTT cache.jdbc.password=TIGER </properties> </loader> </loaders>
As an alternative to configuring the entire JDBC connection, the name of an existing data source can be given:
<loaders passivation="false" shared="false"> <preload> <node fqn="/some/stuff"/> </preload> <!-- if passivation is true, only the first cache loader is used; the rest are ignored --> <loader class="org.jboss.cache.loader.JDBCCacheLoader" async="false" fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false"> <properties> cache.jdbc.datasource=java:/DefaultDS </properties> </loader> </loaders>
Configuration example for a cache loader using c3p0 JDBC connection pooling:
<loaders passivation="false" shared="false"> <preload> <node fqn="/some/stuff"/> </preload> <!-- if passivation is true, only the first cache loader is used; the rest are ignored --> <loader class="org.jboss.cache.loader.JDBCCacheLoader" async="false" fetchPersistentState="true" ignoreModifications="false" purgeOnStartup="false"> <properties> cache.jdbc.table.name=jbosscache cache.jdbc.table.create=true cache.jdbc.table.drop=true cache.jdbc.table.primarykey=jbosscache_pk cache.jdbc.fqn.column=fqn cache.jdbc.fqn.type=VARCHAR(255) cache.jdbc.node.column=node cache.jdbc.node.type=BLOB cache.jdbc.parent.column=parent cache.jdbc.driver=oracle.jdbc.OracleDriver cache.jdbc.url=jdbc:oracle:thin:@localhost:1521:JBOSSDB cache.jdbc.user=SCOTT cache.jdbc.password=TIGER cache.jdbc.connection.factory=org.jboss.cache.loader.C3p0ConnectionFactory c3p0.maxPoolSize=20 c3p0.checkoutTimeout=5000 </properties> </loader> </loaders>