|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.core.DbControl
public final class DbControl
A DbControl
object is the main object used for communicating
with the database. It contains methods for saving and deleting
items, handling transactions, etc. DbControl
objects
are created by the SessionControl.newDbControl()
method.
Nested Class Summary | |
---|---|
private static class |
DbControl.SaveIf
|
Field Summary | |
---|---|
private List<Batcher> |
batchers
A list of batchers that will be closed when DbControl is
closed and will be flushed when commit is called. |
private Throwable |
calledFrom
To keep track of the code that created this DbControl. |
private Map<BasicItem,Transactional.Action> |
commitQueue
A list of items that need special handling when commit is called. |
private SessionWrapper |
hSession
The Hibernate session. |
private StatelessSessionWrapper |
hStatelessSession
A Hibernate stateless session. |
private org.hibernate.Transaction |
hTransaction
The Hibernate transaction. |
private boolean |
isClosed
Is the connection closed or not? |
private boolean |
isConnected
If Hibernate is connected to the database or not. |
private boolean |
isRolledBack
If the current transaction has been rolled back or not. |
private Map<BasicData,BasicItem> |
itemCache
A cache of already loaded items. |
private static Logger |
log
Log core events. |
private LogControl |
logControl
Handles logging of changes to items. |
private Map<BasicItem,List<DbControl.SaveIf>> |
saveIfQueue
Lists items that are saved only if the parent item is also saved. |
private SessionControl |
sc
|
Constructor Summary | |
---|---|
DbControl(SessionControl sc)
Create a new object. |
Method Summary | ||
---|---|---|
(package private) void |
addBatcher(Batcher batcher)
Add a Batcher to the batcher queue. |
|
private void |
cleanUp()
Clear and close all objects. |
|
void |
close()
Close the connection to the database. |
|
void |
commit()
Commit all changes made to items and close the connection to the database. |
|
void |
deleteItem(BasicItem item)
Schedule an existing item to be deleted from the database. |
|
void |
detachItem(BasicItem item)
Detach an item from this DbControl . |
|
void |
disconnect()
Temporarily disconnect from the database. |
|
protected void |
finalize()
Clean up if a bad client application forgot to close the connection. |
|
(package private) long |
getDiskUsage(GroupData group,
QuotaTypeData quotaType,
Location location)
Get the disk usage for the specified group at the specified location and quota type. |
|
(package private) long |
getDiskUsage(UserData user,
QuotaTypeData quotaType,
Location location)
Get the disk usage for the specified user at the specified location and quota type. |
|
(package private) org.hibernate.Session |
getHibernateSession()
Get the underlying Hibernate session. |
|
(package private)
|
getItem(Class<I> itemClass,
BasicData data,
Object... extraParameters)
Get an item object for a known data object, using the cache if possible. |
|
(package private) LogControl |
getLogControl()
Get the log controller for this db control. |
|
SessionControl |
getSessionControl()
Get the SessionControl object that owns
this DbControl object. |
|
(package private) org.hibernate.StatelessSession |
getStatelessSession()
Get a stateless Hibernate session using the same database connection as the regular session. |
|
void |
initCollection(BasicItem item,
String collectionName)
Initialise a collection on an item. |
|
(package private) boolean |
isAttached(BasicItem item)
Check if an item is attached to this DbControl . |
|
boolean |
isClosed()
Check if the connection to the database has been closed. |
|
boolean |
isConnected()
Checks if there is an active connection to the database or not. |
|
boolean |
isRolledBack()
If the transaction was rolled back, either as a call to close()
or because of an error during commit() . |
|
(package private)
|
loadItem(Class<I> itemClass,
int id)
Load an item from the database when you now the id. |
|
(package private)
|
loadItemByDataClass(Class<I> itemClass,
Class<D> dataClass,
int id)
Load an item from the database when you know the id. |
|
(package private)
|
newItem(Class<I> itemClass,
Object... extraParameters)
Create a new item. |
|
void |
reattachItem(BasicItem item)
Deprecated. Use reattachItem(BasicItem, boolean) instead |
|
|
reattachItem(T item,
boolean updated)
Reattach a detached item from this DbControl . |
|
void |
reconnect()
Reconnects to the database after a disconnect() . |
|
void |
refreshItem(BasicItem item)
Reload the item from the database. |
|
(package private) void |
removeBatcher(Batcher batcher)
Remove a Batcher from the batcher queue. |
|
private void |
rollback()
Rollback the transaction. |
|
void |
saveItem(BasicItem item)
Schedule a new item to be saved in the database. |
|
void |
saveItemIf(BasicItem itemIf,
BasicItem item,
boolean before)
Schedule a new item to be saved in the database if another item is also saved. |
|
private void |
updateDiskUsage(BasicItem item)
Update the disk usage information for a DiskConsumable item. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Logger log
private final SessionControl sc
private final Throwable calledFrom
private LogControl logControl
private SessionWrapper hSession
private StatelessSessionWrapper hStatelessSession
private org.hibernate.Transaction hTransaction
private Map<BasicData,BasicItem> itemCache
private Map<BasicItem,Transactional.Action> commitQueue
private Map<BasicItem,List<DbControl.SaveIf>> saveIfQueue
saveItemIf(BasicItem, BasicItem, boolean)
private List<Batcher> batchers
DbControl
is
closed and will be flushed when commit is called.
private boolean isClosed
private boolean isConnected
private boolean isRolledBack
Constructor Detail |
---|
DbControl(SessionControl sc) throws BaseException
BaseException
Method Detail |
---|
public boolean isClosed()
DbControl
object.
SessionControl.newDbControl()
public SessionControl getSessionControl()
SessionControl
object that owns
this DbControl
object.
SessionControl
objectLogControl getLogControl()
org.hibernate.Session getHibernateSession()
org.hibernate.StatelessSession getStatelessSession()
public void disconnect() throws ConnectionClosedException, BaseException
reconnect()
before calling
commit()
. If you have used batch items, for example
ReporterBatcher
, you should call commit()
or you will lose
everything that has already been BasicBatcher.flush()
:ed to the
database.
ConnectionClosedException
- If connection to database has been closed.
BaseException
- If there is another errorpublic void reconnect() throws ConnectionClosedException, BaseException
disconnect()
.
ConnectionClosedException
- If connection to database has been closed.
BaseException
- If there is another error.public boolean isConnected()
public void close()
DbControl
cannot be used anymore.
public boolean isRolledBack()
close()
or because of an error during commit()
. To check if a transaction
was successfully committed use: dc.isClosed() && !dc.isRolledBack()
.
private void rollback()
private void cleanUp()
public void commit() throws BaseException
BaseException
- If there is an errorprivate void updateDiskUsage(BasicItem item) throws QuotaException, BaseException
item
parameter impements the
DiskConsumable
interface and that it's data object implements the
DiskConsumableData
interface.
QuotaException
- If the quota is exceeded
BaseException
- If there is another errorlong getDiskUsage(UserData user, QuotaTypeData quotaType, Location location) throws BaseException
BaseException
long getDiskUsage(GroupData group, QuotaTypeData quotaType, Location location) throws BaseException
BaseException
<I extends BasicItem> I newItem(Class<I> itemClass, Object... extraParameters)
itemClass
- The class of the item
itemClass
class
BaseException
- If there is an error<I extends BasicItem> I loadItem(Class<I> itemClass, int id) throws PermissionDeniedException, BaseException
itemClass
- The Class
of the item objectid
- The id of the object
itemClass
class
PermissionDeniedException
- If the logged in user doesn't have
read permission
BaseException
- If there is another error<I extends BasicItem,D extends BasicData> I loadItemByDataClass(Class<I> itemClass, Class<D> dataClass, int id) throws PermissionDeniedException, BaseException
itemClass
- The item class for the objectdataClass
- The actual data class for the object which must be compatible
with the item classid
- The id of the object
itemClass
class
PermissionDeniedException
BaseException
<I extends BasicItem> I getItem(Class<I> itemClass, BasicData data, Object... extraParameters) throws PermissionDeniedException, ItemNotFoundException, BaseException
itemClass
- The Class
of the item objectdata
- The data object, or null
itemClass
class, or null if null was passed
PermissionDeniedException
- If the logged in user doesn't have
read permission
ItemNotFoundException
- If the data object is a Hibernate proxy
which points to a non-existing row in the database
BaseException
- If there is another errorpublic void saveItem(BasicItem item) throws PermissionDeniedException, ItemAlreadyExistsException, BaseException
commit()
method is called.
item
- The item to be saved
PermissionDeniedException
- If the logged in user doesn't have
create permission
ItemAlreadyExistsException
- If the item already exists in the
database
BaseException
- If there is another errorpublic void saveItemIf(BasicItem itemIf, BasicItem item, boolean before)
itemIf
item is saved then item
is also saved,
otherwise it is not. The method is useful for utility methods that creates a lot
of child items to a parent item and we wan't to wait putting them in the real
save queue until the parent item is saved. If the parent item is already in the
database or saveItem(itemIf) has been called this method is the same as
saveItem(item)
itemIf
- The parent item that must be saveditem
- The item to save when the parent item is savedbefore
- TRUE if the item
should be saved before the itemIf
,
FALSE to save itemIf
firstsaveItem(BasicItem)
public void deleteItem(BasicItem item) throws PermissionDeniedException, ItemNotFoundException, BaseException
commit()
method is called.
If you are deleting multiple items it is better to use
Trashcan.delete(SessionControl, java.util.Collection, boolean, ProgressReporter)
since it will automatically select the best order to delete the items.
item
- The item to be deleted
PermissionDeniedException
- If the logged in user doesn't have
delete permission
ItemNotFoundException
- If the item doesn't exist in the
database
BaseException
- If there is another errorpublic void detachItem(BasicItem item) throws BaseException
DbControl
. The detached item
will no longer be managed and changes will not be saved to the database.
For some items, certain operations are forbidden for detached items. For example
you are not allowed to upload files to File
items.
item
- The item to be detached
BaseException
- If there is an errorreattachItem(BasicItem)
public void reattachItem(BasicItem item) throws PermissionDeniedException, ItemNotFoundException, BaseException
reattachItem(BasicItem, boolean)
instead
PermissionDeniedException
ItemNotFoundException
BaseException
public <T extends BasicItem> T reattachItem(T item, boolean updated) throws PermissionDeniedException, ItemNotFoundException, BaseException
DbControl
. If the
reattached already exists in the database it will be managed and
changes will be saved to the database.
item
- The item to be reattachedupdated
- If TRUE, the item has been updated while it was detached
and changes will be saved to the database. If FALSE, the item will
only be saved to the database if it is modified after the
reattachement
PermissionDeniedException
- If the logged in user doesn't have
read/write permission
BaseException
- If there is another error
ItemNotFoundException
detachItem(BasicItem)
public void refreshItem(BasicItem item)
item
- The item to reloadboolean isAttached(BasicItem item)
DbControl
.
An item is attached if it is found in either the item cache (existing items
only) or commit queue (new or existing items).
public void initCollection(BasicItem item, String collectionName) throws BaseException
Quota
object and calls the
Quota.getQuotaValue(QuotaType, Location)
method.
item
- The itemcollectionName
- The name of the collection
BaseException
- If there is an errordetachItem(BasicItem)
,
reattachItem(BasicItem)
void addBatcher(Batcher batcher)
Batcher
to the batcher queue.
batcher
- The Batcher
to addvoid removeBatcher(Batcher batcher)
Batcher
from the batcher queue.
batcher
- The Batcher
to removeprotected void finalize() throws Throwable
finalize
in class Object
Throwable
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |