Hello
I am using Spring 1.2.6, Hibernate 3.1rc3, Struts.
I have a task to drop and create all database tables and load data into database from file in other words restore data from backup archive at runtime.
Code:
<!-- Backup Manager Implementation -->
<bean id="backupManager" parent="txProxyTemplate" lazy-init="false">
<property name="target">
<bean class="com.blandware.atleap.service.core.impl.BackupManagerImpl">
<property name="backupDAO">
<ref bean="backupDAO"/>
</property>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="backup*">PROPAGATION_REQUIRED</prop>
<prop key="list*">PROPAGATION_REQUIRED</prop>
<prop key="restore*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
I use HibernateDaoSupport.
Code:
<!-- BackupDAO: Hibernate implementation -->
<bean id="backupDAO" class="com.blandware.atleap.persistence.hibernate.core.BackupDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
My restore method do following
Code:
public void restore(final Date date, final Boolean force) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory());
hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER);
hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Connection con = session.connection();
boolean oldAutoCommit = con.getAutoCommit();
if (!oldAutoCommit) {
con.setAutoCommit(true);
}
InputStream is = null;
try {
//read data from archive
...
//drop all tables
...
//create all required tables
...
//load initial data
...
} catch (Exception ex) {
String message = "Cannot restore database with date " + date;
if (log.isWarnEnabled()) {
log.warn(message, ex);
}
throw new HibernateException(message, ex);
} finally {
try {
is.close();
} catch(Exception e) {}
if (!oldAutoCommit) {
con.setAutoCommit(false);
}
}
return null;
}
}
);
}
It works perfectly, but if during execution of this method some other client tries perform some operation I got different exceptions.
How to prevent using any tables during execution of this task?
Thanks beforehand.