01: package com.technoetic.xplanner.upgrade;
02:
03: import java.sql.Connection;
04: import java.sql.SQLException;
05:
06: import net.sf.hibernate.HibernateException;
07: import net.sf.hibernate.Session;
08: import org.apache.log4j.Logger;
09: import org.springframework.dao.DataAccessException;
10: import org.springframework.jdbc.core.ConnectionCallback;
11:
12: import com.technoetic.xplanner.XPlannerProperties;
13: import com.technoetic.xplanner.db.hibernate.GlobalSessionFactory;
14: import com.technoetic.xplanner.db.hibernate.HibernateHelper;
15: import com.technoetic.xplanner.db.hibernate.ThreadSession;
16:
17: //DEBT(AUTOPATCH): Should inherit from spring HibernateDAOSupport.
18: //DEBT(AUTOPATCH): Need a MigrationContext that holds a HibernateSessionFactory instead of a connection
19: //DEBT(AUTOPATCH): Derived class SHOULD NOT Commit themselves. This is because the patch table should be updated inside the same transaction but is not if migration task is doing its own tx management
20:
21: /** @noinspection OverlyBroadCatchBlock*/
22: public abstract class HibernateMigrationTaskSupport extends
23: JdbcMigrationTaskSupport {
24: protected XPlannerProperties properties = new XPlannerProperties();
25:
26: private static final Logger LOG = Logger
27: .getLogger(HibernateMigrationTaskSupport.class);
28: protected Session session;
29:
30: protected HibernateMigrationTaskSupport(String name, int level) {
31: super (name, level);
32: }
33:
34: protected void setUp() throws Exception {
35: template.execute(new ConnectionCallback() {
36: public Object doInConnection(Connection con)
37: throws SQLException, DataAccessException {
38: try {
39: HibernateHelper.initializeHibernate();
40: } catch (HibernateException e) {
41: throw new RuntimeException(e);
42: }
43: session = GlobalSessionFactory.get().openSession(con);
44: ThreadSession.set(session);
45: return null;
46: }
47: });
48: }
49:
50: protected void tearDown() throws Exception {
51: if (session != null && session.isOpen()) {
52: session.flush();
53: session.close();
54: }
55: }
56:
57: protected abstract void migrate() throws Exception;
58:
59: }
|