📄 _baserootdao.java
字号:
return null;
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or using
* the current value of the identifier property if the assigned generator is
* used.)
*/
protected Serializable save(final Object obj)
{
return (Serializable) run(new TransactionRunnable()
{
public Object run(Session s)
{
return save(obj, s);
}
});
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or using
* the current value of the identifier property if the assigned generator is
* used.)
*/
protected Serializable save(Object obj, Session s)
{
return s.save(obj);
}
/**
* Used by the base DAO classes but here for your modification Either save()
* or update() the given instance, depending upon the value of its identifier
* property.
*/
protected void saveOrUpdate(final Object obj)
{
run(new TransactionRunnable()
{
public Object run(Session s)
{
saveOrUpdate(obj, s);
return null;
}
});
}
/**
* Used by the base DAO classes but here for your modification Either save()
* or update() the given instance, depending upon the value of its identifier
* property.
*/
protected void saveOrUpdate(Object obj, Session s)
{
s.saveOrUpdate(obj);
}
/**
* Used by the base DAO classes but here for your modification Update the
* persistent state associated with the given identifier. An exception is
* thrown if there is a persistent instance with the same identifier in the
* current session.
*
* @param obj
* a transient instance containing updated state
*/
protected void update(final Object obj)
{
run(new TransactionRunnable()
{
public Object run(Session s)
{
update(obj, s);
return null;
}
});
}
/**
* Used by the base DAO classes but here for your modification Update the
* persistent state associated with the given identifier. An exception is
* thrown if there is a persistent instance with the same identifier in the
* current session.
*
* @param obj
* a transient instance containing updated state
* @param s
* the Session
*/
protected void update(Object obj, Session s)
{
s.update(obj);
}
/**
* Delete all objects returned by the query
*/
protected int delete(final Query query)
{
Integer rtn = (Integer) run(new TransactionRunnable()
{
public Object run(Session s)
{
return new Integer(delete((Query) query, s));
}
});
return rtn.intValue();
}
/**
* Delete all objects returned by the query
*/
protected int delete(Query query, Session s)
{
List list = query.list();
for (Iterator i = list.iterator(); i.hasNext();)
{
delete(i.next(), s);
}
return list.size();
}
/**
* Used by the base DAO classes but here for your modification Remove a
* persistent instance from the datastore. The argument may be an instance
* associated with the receiving Session or a transient instance with an
* identifier associated with existing persistent state.
*/
protected void delete(final Object obj)
{
run(new TransactionRunnable()
{
public Object run(Session s)
{
delete(obj, s);
return null;
}
});
}
/**
* Used by the base DAO classes but here for your modification Remove a
* persistent instance from the datastore. The argument may be an instance
* associated with the receiving Session or a transient instance with an
* identifier associated with existing persistent state.
*/
protected void delete(Object obj, Session s)
{
s.delete(obj);
}
/**
* Used by the base DAO classes but here for your modification Re-read the
* state of the given instance from the underlying database. It is inadvisable
* to use this to implement long-running sessions that span many business
* tasks. This method is, however, useful in certain special circumstances.
*/
protected void refresh(Object obj, Session s)
{
s.refresh(obj);
}
protected void throwException(Throwable t)
{
if (t instanceof HibernateException)
throw (HibernateException) t;
else if (t instanceof RuntimeException)
throw (RuntimeException) t;
else
throw new HibernateException(t);
}
/**
* Execute the given transaction runnable.
*/
protected Object run(TransactionRunnable transactionRunnable)
{
Transaction t = null;
Session s = null;
try
{
s = getSession();
t = beginTransaction(s);
Object obj = transactionRunnable.run(s);
commitTransaction(t);
return obj;
}
catch (Throwable throwable)
{
if (null != t)
{
try
{
t.rollback();
}
catch (HibernateException e)
{
handleError(e);
}
}
if (transactionRunnable instanceof TransactionFailHandler)
{
try
{
((TransactionFailHandler) transactionRunnable).onFail(s);
}
catch (Throwable e)
{
handleError(e);
}
}
throwException(throwable);
return null;
}
finally
{
closeSession(s);
}
}
/**
* Execute the given transaction runnable.
*/
protected TransactionPointer runAsnyc(TransactionRunnable transactionRunnable)
{
final TransactionPointer transactionPointer = new TransactionPointer(
transactionRunnable);
ThreadRunner threadRunner = new ThreadRunner(transactionPointer);
threadRunner.start();
return transactionPointer;
}
/**
* This class can be used to encapsulate logic used for a single transaction.
*/
public abstract class TransactionRunnable
{
public abstract Object run(Session s) throws Exception;
}
/**
* This class can be used to handle any error that has occured during a
* transaction
*/
public interface TransactionFailHandler
{
public void onFail(Session s);
}
/**
* This class can be used to handle failed transactions
*/
public abstract class TransactionRunnableFailHandler extends
TransactionRunnable implements TransactionFailHandler
{
}
public class TransactionPointer
{
private TransactionRunnable transactionRunnable;
private Throwable thrownException;
private Object returnValue;
private boolean hasCompleted = false;
public TransactionPointer(TransactionRunnable transactionRunnable)
{
this.transactionRunnable = transactionRunnable;
}
public boolean hasCompleted()
{
return hasCompleted;
}
public void complete()
{
this.hasCompleted = true;
}
public Object getReturnValue()
{
return returnValue;
}
public void setReturnValue(Object returnValue)
{
this.returnValue = returnValue;
}
public Throwable getThrownException()
{
return thrownException;
}
public void setThrownException(Throwable thrownException)
{
this.thrownException = thrownException;
}
public TransactionRunnable getTransactionRunnable()
{
return transactionRunnable;
}
public void setTransactionRunnable(TransactionRunnable transactionRunnable)
{
this.transactionRunnable = transactionRunnable;
}
/**
* Wait until the transaction completes and return the value returned from
* the run method of the TransactionRunnable. If the transaction throws an
* Exception, throw that Exception.
*
* @param timeout
* the timeout in milliseconds (or 0 for no timeout)
* @return the return value from the TransactionRunnable
* @throws TimeLimitExcededException
* if the timeout has been reached before transaction completion
* @throws Throwable
* the thrown Throwable
*/
public Object waitUntilFinish(long timeout) throws Throwable
{
long killTime = -1;
if (timeout > 0)
killTime = System.currentTimeMillis() + timeout;
do
{
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
}
}
while (!hasCompleted
&& ((killTime > 0 && System.currentTimeMillis() < killTime) || killTime <= 0));
if (!hasCompleted)
throw new javax.naming.TimeLimitExceededException();
if (null != thrownException)
throw thrownException;
else
return returnValue;
}
}
private class ThreadRunner extends Thread
{
private TransactionPointer transactionPointer;
public ThreadRunner(TransactionPointer transactionPointer)
{
this.transactionPointer = transactionPointer;
}
public void run()
{
Transaction t = null;
Session s = null;
try
{
s = getSession();
t = beginTransaction(s);
Object obj = transactionPointer.getTransactionRunnable().run(s);
t.commit();
transactionPointer.setReturnValue(obj);
}
catch (Throwable throwable)
{
if (null != t)
{
try
{
t.rollback();
}
catch (HibernateException e)
{
handleError(e);
}
}
if (transactionPointer.getTransactionRunnable() instanceof TransactionFailHandler)
{
try
{
((TransactionFailHandler) transactionPointer
.getTransactionRunnable()).onFail(s);
}
catch (Throwable e)
{
handleError(e);
}
}
transactionPointer.setThrownException(throwable);
}
finally
{
transactionPointer.complete();
try
{
closeSession(s);
}
catch (HibernateException e)
{
transactionPointer.setThrownException(e);
}
}
}
}
protected void handleError(Throwable t)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -