Class DbDataStore
- java.lang.Object
-
- org.apache.jackrabbit.core.data.AbstractDataStore
-
- org.apache.jackrabbit.core.data.db.DbDataStore
-
- All Implemented Interfaces:
DataStore
,MultiDataStoreAware
,DatabaseAware
- Direct Known Subclasses:
DerbyDataStore
public class DbDataStore extends AbstractDataStore implements DatabaseAware, MultiDataStoreAware
A data store implementation that stores the records in a database using JDBC. Configuration:<DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore"> <param name="
url
" value="jdbc:postgresql:test"/> <param name="user
" value="sa"/> <param name="password
" value="sa"/> <param name="databaseType
" value="postgresql"/> <param name="driver
" value="org.postgresql.Driver"/> <param name="minRecordLength
" value="1024"/> <param name="maxConnections
" value="2"/> <param name="copyWhenReading
" value="true"/> <param name="tablePrefix
" value=""/> <param name="schemaObjectPrefix
" value=""/> <param name="schemaCheckEnabled
" value="true"/> </DataStore>Only URL, user name and password usually need to be set. The remaining settings are generated using the database URL sub-protocol from the database type resource file.
JNDI can be used to get the connection. In this case, use the javax.naming.InitialContext as the driver, and the JNDI name as the URL. If the user and password are configured in the JNDI resource, they should not be configured here. Example JNDI settings:
<param name="driver" value="javax.naming.InitialContext" /> <param name="url" value="java:comp/env/jdbc/Test" />
For Microsoft SQL Server 2005, there is a problem reading large BLOBs. You will need to use the JDBC driver version 1.2 or newer, and append ;responseBuffering=adaptive to the database URL. Don't append ;selectMethod=cursor, otherwise it can still run out of memory. Example database URL: jdbc:sqlserver://localhost:4220;DatabaseName=test;responseBuffering=adaptive
By default, the data is copied to a temp file when reading, to avoid problems when reading multiple blobs at the same time.
The tablePrefix can be used to specify a schema and / or catalog name: <param name="tablePrefix" value="ds.">
-
-
Field Summary
Fields Modifier and Type Field Description protected ConnectionHelper
conHelper
TheConnectionHelper
set in theinit(String)
method.protected boolean
copyWhenReading
Copy the stream to a temp file before returning it.protected String
createTableSQL
This is the property 'createTable' in the [databaseType].properties file, initialized with the default value.protected String
databaseType
The database type used.protected String
dataSourceName
The logical name of the DataSource to use.static int
DEFAULT_MIN_RECORD_LENGTH
The default value for the minimum object size.protected String
deleteOlderSQL
This is the property 'deleteOlder' in the [databaseType].properties file, initialized with the default value.protected String
deleteSQL
This is the property 'delete' in the [databaseType].properties file, initialized with the default value.protected static String
DIGEST
The digest algorithm used to uniquely identify records.protected String
driver
The database driver.protected String
insertTempSQL
This is the property 'insertTemp' in the [databaseType].properties file, initialized with the default value.protected Map<DataIdentifier,WeakReference<DataIdentifier>>
inUse
All data identifiers that are currently in use are in this set until they are garbage collected.protected long
minModifiedDate
The minimum modified date.protected int
minRecordLength
The minimum size of an object that should be stored in this data store.protected String
password
The passwordprotected String
schemaObjectPrefix
The prefix of the table names.protected String
selectAllSQL
This is the property 'selectAll' in the [databaseType].properties file, initialized with the default value.protected String
selectDataSQL
This is the property 'selectData' in the [databaseType].properties file, initialized with the default value.protected String
selectMetaSQL
This is the property 'selectMeta' in the [databaseType].properties file, initialized with the default value.static String
STORE_SIZE_MAX
Call PreparedStatement.setBinaryStream(..., Integer.MAX_VALUE)static String
STORE_SIZE_MINUS_ONE
Call PreparedStatement.setBinaryStream(..., -1)static String
STORE_TEMP_FILE
Write to a temporary file to get the length (slow, but always works).protected String
storeStream
The stream storing mechanism used.protected String
tablePrefix
The prefix for the datastore table, empty by default.protected String
tableSQL
This is the property 'table' in the [databaseType].properties file, initialized with the default value.protected static String
TEMP_PREFIX
The prefix used for temporary objects.protected List<String>
temporaryInUse
The temporary identifiers that are currently in use.protected String
updateDataSQL
This is the property 'updateData' in the [databaseType].properties file, initialized with the default value.protected String
updateLastModifiedSQL
This is the property 'updateLastModified' in the [databaseType].properties file, initialized with the default value.protected String
updateSQL
This is the property 'update' in the [databaseType].properties file, initialized with the default value.protected String
url
The database URL used.protected String
user
The user name.
-
Constructor Summary
Constructors Constructor Description DbDataStore()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description DataRecord
addRecord(InputStream stream)
Creates a new data record.void
clearInUse()
Clear the in-use list.void
close()
Close the data storeprotected DataStoreException
convert(String cause, Exception e)
Convert an exception to a data store exception.protected CheckSchemaOperation
createCheckSchemaOperation()
This method is called frominit(String)
after thecreateConnectionHelper(DataSource)
method, and returns a defaultCheckSchemaOperation
.protected ConnectionHelper
createConnectionHelper(DataSource dataSrc)
This method is called from theinit(String)
method of this class and returns aConnectionHelper
instance which is assigned to theconHelper
field.int
deleteAllOlderThan(long min)
Delete objects that have a modified date older than the specified date.void
deleteRecord(DataIdentifier identifier)
Deletes a single DataRecord based on the given identifier.Iterator<DataIdentifier>
getAllIdentifiers()
Get all identifiers.boolean
getCopyWhenReading()
Is a stream copied to a temporary file before returning?String
getDatabaseType()
Get the database type (if set).String
getDataSourceName()
protected MessageDigest
getDigest()
String
getDriver()
Get the database driverint
getMaxConnections()
Deprecated.int
getMinRecordLength()
Get the minimum size of an object that should be stored in this data store.String
getPassword()
Get the password.protected String
getProperty(Properties prop, String key, String defaultValue)
Get the expanded property value.DataRecord
getRecordIfStored(DataIdentifier identifier)
Check if a record for the given identifier exists, and return it if yes.String
getSchemaObjectPrefix()
Get the schema prefix.String
getTablePrefix()
Get the table prefix.String
getUrl()
Get the database URL.String
getUser()
Get the user name.void
init(String homeDir)
Initialized the data storeprotected void
initDatabaseType()
boolean
isSchemaCheckEnabled()
void
setConnectionFactory(ConnectionFactory connnectionFactory)
void
setCopyWhenReading(boolean copyWhenReading)
The the copy setting.void
setDatabaseType(String databaseType)
Set the database type.void
setDataSourceName(String dataSourceName)
void
setDriver(String driver)
Set the database driver class name.void
setMaxConnections(int maxConnections)
Deprecated.void
setMinRecordLength(int minRecordLength)
Set the minimum object length.void
setPassword(String password)
Set the password.void
setSchemaCheckEnabled(boolean enabled)
void
setSchemaObjectPrefix(String schemaObjectPrefix)
Set the schema object prefix.void
setTablePrefix(String tablePrefix)
Set the new table prefix.void
setUrl(String url)
Set the database URL.void
setUser(String user)
Set the user name.void
updateModifiedDateOnAccess(long before)
From now on, update the modified date of an object even when accessing it.protected void
usesIdentifier(DataIdentifier identifier)
-
Methods inherited from class org.apache.jackrabbit.core.data.AbstractDataStore
encodeHexString, getOrCreateReferenceKey, getRecord, getRecordFromReference, getReferenceFromIdentifier
-
-
-
-
Field Detail
-
DEFAULT_MIN_RECORD_LENGTH
public static final int DEFAULT_MIN_RECORD_LENGTH
The default value for the minimum object size.- See Also:
- Constant Field Values
-
STORE_TEMP_FILE
public static final String STORE_TEMP_FILE
Write to a temporary file to get the length (slow, but always works). This is the default setting.- See Also:
- Constant Field Values
-
STORE_SIZE_MINUS_ONE
public static final String STORE_SIZE_MINUS_ONE
Call PreparedStatement.setBinaryStream(..., -1)- See Also:
- Constant Field Values
-
STORE_SIZE_MAX
public static final String STORE_SIZE_MAX
Call PreparedStatement.setBinaryStream(..., Integer.MAX_VALUE)- See Also:
- Constant Field Values
-
DIGEST
protected static final String DIGEST
The digest algorithm used to uniquely identify records.
-
TEMP_PREFIX
protected static final String TEMP_PREFIX
The prefix used for temporary objects.- See Also:
- Constant Field Values
-
minModifiedDate
protected long minModifiedDate
The minimum modified date. If a file is accessed (read or write) with a modified date older than this value, the modified date is updated to the current time.
-
url
protected String url
The database URL used.
-
driver
protected String driver
The database driver.
-
user
protected String user
The user name.
-
password
protected String password
The password
-
databaseType
protected String databaseType
The database type used.
-
minRecordLength
protected int minRecordLength
The minimum size of an object that should be stored in this data store.
-
tablePrefix
protected String tablePrefix
The prefix for the datastore table, empty by default.
-
schemaObjectPrefix
protected String schemaObjectPrefix
The prefix of the table names. By default it is empty.
-
dataSourceName
protected String dataSourceName
The logical name of the DataSource to use.
-
tableSQL
protected String tableSQL
This is the property 'table' in the [databaseType].properties file, initialized with the default value.
-
createTableSQL
protected String createTableSQL
This is the property 'createTable' in the [databaseType].properties file, initialized with the default value.
-
insertTempSQL
protected String insertTempSQL
This is the property 'insertTemp' in the [databaseType].properties file, initialized with the default value.
-
updateDataSQL
protected String updateDataSQL
This is the property 'updateData' in the [databaseType].properties file, initialized with the default value.
-
updateLastModifiedSQL
protected String updateLastModifiedSQL
This is the property 'updateLastModified' in the [databaseType].properties file, initialized with the default value.
-
updateSQL
protected String updateSQL
This is the property 'update' in the [databaseType].properties file, initialized with the default value.
-
deleteSQL
protected String deleteSQL
This is the property 'delete' in the [databaseType].properties file, initialized with the default value.
-
deleteOlderSQL
protected String deleteOlderSQL
This is the property 'deleteOlder' in the [databaseType].properties file, initialized with the default value.
-
selectMetaSQL
protected String selectMetaSQL
This is the property 'selectMeta' in the [databaseType].properties file, initialized with the default value.
-
selectAllSQL
protected String selectAllSQL
This is the property 'selectAll' in the [databaseType].properties file, initialized with the default value.
-
selectDataSQL
protected String selectDataSQL
This is the property 'selectData' in the [databaseType].properties file, initialized with the default value.
-
storeStream
protected String storeStream
The stream storing mechanism used.
-
copyWhenReading
protected boolean copyWhenReading
Copy the stream to a temp file before returning it. Enabled by default to support concurrent reads.
-
inUse
protected Map<DataIdentifier,WeakReference<DataIdentifier>> inUse
All data identifiers that are currently in use are in this set until they are garbage collected.
-
temporaryInUse
protected List<String> temporaryInUse
The temporary identifiers that are currently in use.
-
conHelper
protected ConnectionHelper conHelper
TheConnectionHelper
set in theinit(String)
method.
-
-
Method Detail
-
setConnectionFactory
public void setConnectionFactory(ConnectionFactory connnectionFactory)
- Specified by:
setConnectionFactory
in interfaceDatabaseAware
-
addRecord
public DataRecord addRecord(InputStream stream) throws DataStoreException
Description copied from interface:DataStore
Creates a new data record. The given binary stream is consumed and a binary record containing the consumed stream is created and returned. If the same stream already exists in another record, then that record is returned instead of creating a new one.The given stream is consumed and not closed by this method. It is the responsibility of the caller to close the stream. A typical call pattern would be:
InputStream stream = ...; try { record = store.addRecord(stream); } finally { stream.close(); }
- Specified by:
addRecord
in interfaceDataStore
- Parameters:
stream
- binary stream- Returns:
- data record that contains the given stream
- Throws:
DataStoreException
- if the data store could not be accessed
-
deleteRecord
public void deleteRecord(DataIdentifier identifier) throws DataStoreException
Description copied from interface:MultiDataStoreAware
Deletes a single DataRecord based on the given identifier. Delete will only be used by theMultiDataStore.MoveDataTask
.- Specified by:
deleteRecord
in interfaceMultiDataStoreAware
- Parameters:
identifier
- data identifier- Throws:
DataStoreException
- if the data store could not be accessed, or if the given identifier is invalid
-
deleteAllOlderThan
public int deleteAllOlderThan(long min) throws DataStoreException
Description copied from interface:DataStore
Delete objects that have a modified date older than the specified date.- Specified by:
deleteAllOlderThan
in interfaceDataStore
- Parameters:
min
- the minimum time- Returns:
- the number of data records deleted
- Throws:
DataStoreException
-
getAllIdentifiers
public Iterator<DataIdentifier> getAllIdentifiers() throws DataStoreException
Description copied from interface:DataStore
Get all identifiers.- Specified by:
getAllIdentifiers
in interfaceDataStore
- Returns:
- an iterator over all DataIdentifier objects
- Throws:
DataStoreException
- if the list could not be read
-
getMinRecordLength
public int getMinRecordLength()
Description copied from interface:DataStore
Get the minimum size of an object that should be stored in this data store. Depending on the overhead and configuration, each store may return a different value.- Specified by:
getMinRecordLength
in interfaceDataStore
- Returns:
- the minimum size in bytes
-
setMinRecordLength
public void setMinRecordLength(int minRecordLength)
Set the minimum object length. The maximum value is around 32000.- Parameters:
minRecordLength
- the length
-
getRecordIfStored
public DataRecord getRecordIfStored(DataIdentifier identifier) throws DataStoreException
Description copied from interface:DataStore
Check if a record for the given identifier exists, and return it if yes. If no record exists, this method returns null.- Specified by:
getRecordIfStored
in interfaceDataStore
- Parameters:
identifier
- data identifier- Returns:
- the record if found, and null if not
- Throws:
DataStoreException
- if the data store could not be accessed
-
init
public void init(String homeDir) throws DataStoreException
Description copied from interface:DataStore
Initialized the data store- Specified by:
init
in interfaceDataStore
- Parameters:
homeDir
- the home directory of the repository- Throws:
DataStoreException
-
createConnectionHelper
protected ConnectionHelper createConnectionHelper(DataSource dataSrc) throws Exception
This method is called from theinit(String)
method of this class and returns aConnectionHelper
instance which is assigned to theconHelper
field. Subclasses may override it to return a specialized connection helper.- Parameters:
dataSrc
- theDataSource
of this persistence manager- Returns:
- a
ConnectionHelper
- Throws:
Exception
- on error
-
createCheckSchemaOperation
protected final CheckSchemaOperation createCheckSchemaOperation()
This method is called frominit(String)
after thecreateConnectionHelper(DataSource)
method, and returns a defaultCheckSchemaOperation
.- Returns:
- a new
CheckSchemaOperation
instance
-
initDatabaseType
protected void initDatabaseType() throws DataStoreException
- Throws:
DataStoreException
-
getProperty
protected String getProperty(Properties prop, String key, String defaultValue)
Get the expanded property value. The following placeholders are supported: ${table}: the table name (the default is DATASTORE) and ${tablePrefix}: tablePrefix plus schemaObjectPrefix as set in the configuration- Parameters:
prop
- the properties objectkey
- the keydefaultValue
- the default value- Returns:
- the property value (placeholders are replaced)
-
convert
protected DataStoreException convert(String cause, Exception e)
Convert an exception to a data store exception.- Parameters:
cause
- the messagee
- the root cause- Returns:
- the data store exception
-
updateModifiedDateOnAccess
public void updateModifiedDateOnAccess(long before)
Description copied from interface:DataStore
From now on, update the modified date of an object even when accessing it. Usually, the modified date is only updated when creating a new object, or when a new link is added to an existing object. When this setting is enabled, even getLength() will update the modified date.- Specified by:
updateModifiedDateOnAccess
in interfaceDataStore
- Parameters:
before
- - update the modified date to the current time if it is older than this value
-
getDatabaseType
public String getDatabaseType()
Get the database type (if set).- Returns:
- the database type
-
setDatabaseType
public void setDatabaseType(String databaseType)
Set the database type. By default the sub-protocol of the JDBC database URL is used if it is not set. It must match the resource file [databaseType].properties. Example: mysql.- Parameters:
databaseType
-
-
getDriver
public String getDriver()
Get the database driver- Returns:
- the driver
-
setDriver
public void setDriver(String driver)
Set the database driver class name. If not set, the default driver class name for the database type is used, as set in the [databaseType].properties resource; key 'driver'.- Parameters:
driver
-
-
getPassword
public String getPassword()
Get the password.- Returns:
- the password
-
setPassword
public void setPassword(String password)
Set the password.- Parameters:
password
-
-
getUrl
public String getUrl()
Get the database URL.- Returns:
- the URL
-
setUrl
public void setUrl(String url)
Set the database URL. Example: jdbc:postgresql:test- Parameters:
url
-
-
getUser
public String getUser()
Get the user name.- Returns:
- the user name
-
setUser
public void setUser(String user)
Set the user name.- Parameters:
user
-
-
isSchemaCheckEnabled
public final boolean isSchemaCheckEnabled()
- Returns:
- whether the schema check is enabled
-
setSchemaCheckEnabled
public final void setSchemaCheckEnabled(boolean enabled)
- Parameters:
enabled
- set whether the schema check is enabled
-
close
public void close() throws DataStoreException
Description copied from interface:DataStore
Close the data store- Specified by:
close
in interfaceDataStore
- Throws:
DataStoreException
- if a problem occurred
-
usesIdentifier
protected void usesIdentifier(DataIdentifier identifier)
-
clearInUse
public void clearInUse()
Description copied from interface:DataStore
Clear the in-use list. This is only used for testing to make the the garbage collection think that objects are no longer in use.- Specified by:
clearInUse
in interfaceDataStore
-
getDigest
protected MessageDigest getDigest() throws DataStoreException
- Throws:
DataStoreException
-
getMaxConnections
public int getMaxConnections()
Deprecated.Get the maximum number of concurrent connections.- Returns:
- the maximum number of connections.
-
setMaxConnections
public void setMaxConnections(int maxConnections)
Deprecated.Set the maximum number of concurrent connections in the pool. At least 3 connections are required if the garbage collection process is used.- Parameters:
maxConnections
- the new value
-
getCopyWhenReading
public boolean getCopyWhenReading()
Is a stream copied to a temporary file before returning?- Returns:
- the setting
-
setCopyWhenReading
public void setCopyWhenReading(boolean copyWhenReading)
The the copy setting. If enabled, a stream is always copied to a temporary file when reading a stream.- Parameters:
copyWhenReading
- the new setting
-
getTablePrefix
public String getTablePrefix()
Get the table prefix.- Returns:
- the table prefix.
-
setTablePrefix
public void setTablePrefix(String tablePrefix)
Set the new table prefix. The default is empty. The table name is constructed like this: ${tablePrefix}${schemaObjectPrefix}${tableName}- Parameters:
tablePrefix
- the new value
-
getSchemaObjectPrefix
public String getSchemaObjectPrefix()
Get the schema prefix.- Returns:
- the schema object prefix
-
setSchemaObjectPrefix
public void setSchemaObjectPrefix(String schemaObjectPrefix)
Set the schema object prefix. The default is empty. The table name is constructed like this: ${tablePrefix}${schemaObjectPrefix}${tableName}- Parameters:
schemaObjectPrefix
- the new prefix
-
getDataSourceName
public String getDataSourceName()
-
setDataSourceName
public void setDataSourceName(String dataSourceName)
-
-