mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-08 02:24:18 +00:00
Adds support for read-only contexts that bypass internal DSO cache
This commit is contained in:
@@ -41,6 +41,9 @@ public class Context
|
|||||||
{
|
{
|
||||||
private static final Logger log = Logger.getLogger(Context.class);
|
private static final Logger log = Logger.getLogger(Context.class);
|
||||||
|
|
||||||
|
/** option flags */
|
||||||
|
public static final short READ_ONLY = 0x01;
|
||||||
|
|
||||||
/** Database connection */
|
/** Database connection */
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
@@ -77,14 +80,42 @@ public class Context
|
|||||||
/** Event dispatcher name */
|
/** Event dispatcher name */
|
||||||
private String dispName = null;
|
private String dispName = null;
|
||||||
|
|
||||||
|
/** options */
|
||||||
|
private short options = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new context object. A database connection is opened. No user
|
* Construct a new context object with default options. A database connection is opened.
|
||||||
* is authenticated.
|
* No user is authenticated.
|
||||||
*
|
*
|
||||||
* @exception SQLException
|
* @exception SQLException
|
||||||
* if there was an error obtaining a database connection
|
* if there was an error obtaining a database connection
|
||||||
*/
|
*/
|
||||||
public Context() throws SQLException
|
public Context() throws SQLException
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new context object with passed options. A database connection is opened.
|
||||||
|
* No user is authenticated.
|
||||||
|
*
|
||||||
|
* @param options context operation flags
|
||||||
|
* @exception SQLException
|
||||||
|
* if there was an error obtaining a database connection
|
||||||
|
*/
|
||||||
|
public Context(short options) throws SQLException
|
||||||
|
{
|
||||||
|
this.options = options;
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new context object.
|
||||||
|
*
|
||||||
|
* @exception SQLException
|
||||||
|
* if there was an error obtaining a database connection
|
||||||
|
*/
|
||||||
|
private void init() throws SQLException
|
||||||
{
|
{
|
||||||
// Obtain a non-auto-committing connection
|
// Obtain a non-auto-committing connection
|
||||||
connection = DatabaseManager.getConnection();
|
connection = DatabaseManager.getConnection();
|
||||||
@@ -292,8 +323,11 @@ public class Context
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Commit any changes made as part of the transaction
|
// Commit any changes made as part of the transaction
|
||||||
|
if (! isReadOnly())
|
||||||
|
{
|
||||||
commit();
|
commit();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// Free the connection
|
// Free the connection
|
||||||
@@ -313,6 +347,14 @@ public class Context
|
|||||||
*/
|
*/
|
||||||
public void commit() throws SQLException
|
public void commit() throws SQLException
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* invalid condition if in read-only mode: no valid
|
||||||
|
* transactions can be committed: no recourse but to bail
|
||||||
|
*/
|
||||||
|
if (isReadOnly())
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Attempt to commit transaction in read-only context");
|
||||||
|
}
|
||||||
// Commit any changes made as part of the transaction
|
// Commit any changes made as part of the transaction
|
||||||
Dispatcher dispatcher = null;
|
Dispatcher dispatcher = null;
|
||||||
|
|
||||||
@@ -368,6 +410,14 @@ public class Context
|
|||||||
*/
|
*/
|
||||||
public void addEvent(Event event)
|
public void addEvent(Event event)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* invalid condition if in read-only mode: events - which
|
||||||
|
* indicate mutation - are firing: no recourse but to bail
|
||||||
|
*/
|
||||||
|
if (isReadOnly())
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Attempt to mutate object in read-only context");
|
||||||
|
}
|
||||||
if (events == null)
|
if (events == null)
|
||||||
{
|
{
|
||||||
events = new LinkedList<Event>();
|
events = new LinkedList<Event>();
|
||||||
@@ -418,10 +468,13 @@ public class Context
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!connection.isClosed())
|
if (!connection.isClosed())
|
||||||
|
{
|
||||||
|
if (! isReadOnly())
|
||||||
{
|
{
|
||||||
connection.rollback();
|
connection.rollback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (SQLException se)
|
catch (SQLException se)
|
||||||
{
|
{
|
||||||
log.error(se.getMessage(), se);
|
log.error(se.getMessage(), se);
|
||||||
@@ -459,6 +512,17 @@ public class Context
|
|||||||
return (connection != null);
|
return (connection != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reports whether context supports updating DSpaceObjects, or only reading.
|
||||||
|
*
|
||||||
|
* @return <code>true</code> if the context is read-only, otherwise
|
||||||
|
* <code>false</code>
|
||||||
|
*/
|
||||||
|
public boolean isReadOnly()
|
||||||
|
{
|
||||||
|
return (options & READ_ONLY) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store an object in the object cache.
|
* Store an object in the object cache.
|
||||||
*
|
*
|
||||||
@@ -486,10 +550,14 @@ public class Context
|
|||||||
* the object's ID
|
* the object's ID
|
||||||
*/
|
*/
|
||||||
public void cache(Object o, int id)
|
public void cache(Object o, int id)
|
||||||
|
{
|
||||||
|
// bypass cache if in read-only mode
|
||||||
|
if (! isReadOnly())
|
||||||
{
|
{
|
||||||
String key = o.getClass().getName() + id;
|
String key = o.getClass().getName() + id;
|
||||||
objectCache.put(key, o);
|
objectCache.put(key, o);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove an object from the object cache.
|
* Remove an object from the object cache.
|
||||||
|
Reference in New Issue
Block a user