Changeset 4503


Ignore:
Timestamp:
Sep 10, 2008, 2:43:46 PM (15 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1087: Update to Hibernate 3.3.0

Location:
trunk/src/core/net/sf/basedb/core
Files:
6 added
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/AbstractBatcher.java

    r4479 r4503  
    2828import java.lang.ref.WeakReference;
    2929import java.sql.SQLException;
    30 import java.sql.Statement;
    3130
    3231import net.sf.basedb.core.dbengine.DbEngine;
     32import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    3333
    3434/**
     
    198198        try
    199199        {
    200           Statement s = HibernateUtil.getConnection(getDbControl().getHibernateSession()).createStatement();
    201           s.executeUpdate(sql);
    202           s.close();
     200          HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(), new ExecuteUpdateWork(sc, sql));
    203201        }
    204202        catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/AbstractSqlQuery.java

    r4479 r4503  
    2626package net.sf.basedb.core;
    2727
     28import net.sf.basedb.core.hibernate.ResultSetWork;
    2829import net.sf.basedb.core.query.SqlQuery;
    2930import net.sf.basedb.core.query.QueryType;
     
    102103    throws BaseException
    103104  {
    104     int totalCount = -1;
     105    long totalCount = -1;
    105106    // Get query string
    106107    String countSql = getCountQuery(dc, true);
    107108    // Parse SQL and replace named parameters with ?
    108     List<String> countParameterOrder = new LinkedList<String>();
     109    final List<String> countParameterOrder = new LinkedList<String>();
     110    final AbstractSqlQuery query = this;
    109111    countSql = parseParameters(countSql, countParameterOrder);
    110112    try
    111113    {
    112       Connection c = HibernateUtil.getConnection(dc.getHibernateSession());
    113114      if (debugSqlEnabled) logSql.debug("Executing count query: " + countSql);
    114       final PreparedStatement ps = c.prepareStatement(countSql);
    115       setParameters(ps, countParameterOrder);
    116115     
    117       ResultSet result = QueryExecutor.executeQuery(ps, dc.getSessionControl());
    118 
    119       result.next();
    120       totalCount = result.getInt(1);
    121       result.close();
    122       ps.close();
    123     }
    124     catch (InterruptedException ex)
    125     {
    126       throw new SignalException("Aborted by user", ex);
     116      totalCount = HibernateUtil.doJdbcWork(dc.getHibernateSession(),
     117        new ResultSetWork<Long>(dc.getSessionControl(), countSql)
     118        {
     119          @Override
     120          protected void setParameters(PreparedStatement ps)
     121            throws SQLException
     122          {
     123            query.setParameters(ps, countParameterOrder);
     124          }
     125          @Override
     126          protected Long getResult(ResultSet rs)
     127            throws SQLException
     128          {
     129            return rs.next() ? rs.getLong(1) : 0;
     130          }
     131        }
     132      );
    127133    }
    128134    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/Application.java

    r4483 r4503  
    2929import net.sf.basedb.core.data.ClientData;
    3030import net.sf.basedb.core.data.SchemaVersionData;
     31import net.sf.basedb.core.hibernate.JdbcWork;
    3132import net.sf.basedb.core.authentication.Authenticator;
    3233import net.sf.basedb.util.FileUtil;
     
    5051import java.util.regex.Pattern;
    5152import java.net.InetAddress;
     53import java.sql.Connection;
    5254import java.sql.DatabaseMetaData;
     55import java.sql.SQLException;
    5356
    5457
     
    297300      {
    298301        session = HibernateUtil.newSession();
    299         DatabaseMetaData meta = HibernateUtil.getConnection(session).getMetaData();
    300         databaseVersionNumber = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
     302        HibernateUtil.doJdbcWork(session,
     303            new JdbcWork<String>()
     304            {
     305              @Override
     306              public String getResult()
     307              {
     308                return null;
     309              }
     310              @Override
     311              public void execute(Connection connection)
     312                throws SQLException
     313              {
     314                DatabaseMetaData meta = connection.getMetaData();
     315                databaseVersionNumber = meta.getDatabaseProductName() + " " + meta.getDatabaseProductVersion();
     316              }
     317            });
    301318      }
    302319      catch (Throwable th)
  • trunk/src/core/net/sf/basedb/core/FeatureBatcher.java

    r4479 r4503  
    3131import net.sf.basedb.core.data.ReporterData;
    3232import net.sf.basedb.core.data.WellData;
    33 import net.sf.basedb.core.signal.SignalException;
     33import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    3434
    3535import java.sql.PreparedStatement;
     
    327327    {
    328328      if (debugSqlEnabled) logSql.debug("Deleting features: " + sql);
    329       PreparedStatement ps = HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(sql);
    330       ps.setInt(1, arrayDesign.getId());
    331       QueryExecutor.executeUpdate(ps, getSessionControl());
     329      HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     330        new ExecuteUpdateWork(getSessionControl(), sql)
     331        {
     332          @Override
     333          public void setParameters(PreparedStatement ps)
     334            throws SQLException
     335          {
     336            ps.setInt(1, arrayDesign.getId());
     337          }
     338         
     339        }
     340      );
    332341      arrayDesignData.setHasFeatures(false);
    333     }
    334     catch (InterruptedException ex)
    335     {
    336       throw new SignalException("Aborted by user.", ex);
    337342    }
    338343    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/FilterBatcher.java

    r4479 r4503  
    2626package net.sf.basedb.core;
    2727
     28import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2829import net.sf.basedb.core.query.Expressions;
    2930import net.sf.basedb.core.query.Selects;
    30 import net.sf.basedb.core.signal.SignalException;
    3131
    3232import java.util.List;
     
    287287    @throws BaseException If there is an error
    288288  */
    289   public int insert(AbstractSqlQuery query)
     289  public int insert(final AbstractSqlQuery query)
    290290    throws BaseException
    291291  {
     
    313313   
    314314    // Holds the names of the parameters in the query in the order they appear
    315     List<String> parameterOrder = new LinkedList<String>();
     315    final List<String> parameterOrder = new LinkedList<String>();
    316316   
    317317    // Parse out named parameters
     
    323323    try
    324324    {
    325       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    326       query.setParameters(ps, parameterOrder);
    327       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    328       ps.close();
     325      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     326        new ExecuteUpdateWork(getSessionControl(), insertSql)
     327        {
     328          @Override
     329          public void setParameters(PreparedStatement ps)
     330            throws SQLException
     331          {
     332            query.setParameters(ps, parameterOrder);
     333          }
     334         
     335        }
     336      );
    329337      bytes += rowsInserted * bytesPerRow;
    330338      totalInsertCount += rowsInserted;
    331     }
    332     catch (InterruptedException ex)
    333     {
    334       throw new SignalException("Aborted by user.", ex);
    335339    }
    336340    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r4479 r4503  
    4242import net.sf.basedb.core.dbengine.TableInfo.IndexInfo;
    4343import net.sf.basedb.core.hibernate.EntityQueryWrapper;
     44import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
     45import net.sf.basedb.core.hibernate.JdbcWork;
     46import net.sf.basedb.core.hibernate.MultiUpdateWork;
     47import net.sf.basedb.core.hibernate.TableExistsWork;
    4448import net.sf.basedb.core.query.QueryType;
    4549import net.sf.basedb.util.XMLUtil;
    4650
     51import java.util.ArrayList;
    4752import java.util.Arrays;
    4853import java.util.HashMap;
     
    540545    {
    541546      session = HibernateUtil.newSession();
    542       DatabaseMetaData metaData = HibernateUtil.getConnection(session).getMetaData();
     547      isEmpty = HibernateUtil.doJdbcWork(session,
     548          new JdbcWork<Boolean>()
     549          {
     550            private boolean isEmpty = true;
     551            @Override
     552            public Boolean getResult()
     553            {
     554              return isEmpty;
     555            }
     556            @Override
     557            public void execute(Connection connection)
     558              throws SQLException
     559            {
     560              DatabaseMetaData metaData = connection.getMetaData();
     561              Iterator<PersistentClass> classes = cfg.getClassMappings();
     562              while (classes.hasNext() && isEmpty)
     563              {
     564                PersistentClass pClass = classes.next();
     565                Table t = pClass.getTable();
     566                ResultSet tables = metaData.getTables(t.getCatalog(), t.getSchema(), t.getName(), new String[] { "TABLE" } );
     567                if (tables.next())
     568                {
     569                  isEmpty = false;
     570                  log.error("Table '"+t.getName()+"' already exists; install aborted");
     571                }
     572                tables.close();
     573              }
     574            }
     575          }
     576        );
    543577     
    544       Iterator<PersistentClass> classes = cfg.getClassMappings();
    545       while (classes.hasNext() && isEmpty)
    546       {
    547         PersistentClass pClass = classes.next();
    548         Table t = pClass.getTable();
    549         ResultSet tables = metaData.getTables(t.getCatalog(), t.getSchema(), t.getName(), new String[] { "TABLE" } );
    550         if (tables.next())
    551         {
    552           isEmpty = false;
    553           log.error("Table '"+t.getName()+"' already exists; install aborted");
    554         }
    555         tables.close();
    556       }
    557578      HibernateUtil.close(session);
    558579      session = null;
     
    600621      session = HibernateUtil.newSession();
    601622      tx = HibernateUtil.newTransaction(session);
    602       Connection connection = HibernateUtil.getConnection(session);
    603623      String tableName = table.getTableName(db);
    604624
    605       if (!tableExists(tableName, connection))
     625      if (!tableExists(tableName, session))
    606626      {
    607627        Table hibernateTable = newDynamicMapping(db, table);
     
    612632        String quotedSchema = quote(dynamicSchema);
    613633
     634        List<String> statements = new ArrayList<String>();
     635
    614636        String sql = hibernateTable.sqlCreateString(dialect, null, quotedCatalog, quotedSchema);
    615637        sql = dbEngine.makeSafeCreateTable(sql, dynamicCatalog, dynamicSchema, tableName);
    616638        logSql.debug(sql);
     639        statements.add(sql);
    617640       
    618         Statement s = connection.createStatement();
    619         s.executeUpdate(sql);
    620 
    621641        Iterator i = hibernateTable.getIndexIterator();
    622642        while (i.hasNext())
     
    625645          sql = ii.sqlCreateString(dialect, null, quotedCatalog, quotedSchema);
    626646          logSql.debug(sql);
    627           s.executeUpdate(sql);
     647          statements.add(sql);
    628648        }
    629         s.close();
     649        HibernateUtil.doJdbcWork(session, new MultiUpdateWork(statements));
    630650      }
    631651      HibernateUtil.commit(tx);
     
    676696      tx = HibernateUtil.newTransaction(session);
    677697
    678       Connection connection = HibernateUtil.getConnection(session);
    679       if (tableExists(table.getTableName(db), connection))
     698      if (tableExists(table.getTableName(db), session))
    680699      {
    681700        Table hibernateTable = newDynamicMapping(db, table);
     
    685704        String sql = hibernateTable.sqlDropString(dialect, dynamicCatalog, dynamicSchema);
    686705        logSql.debug(sql);
    687        
    688         Statement s = connection.createStatement();
    689         s.executeUpdate(sql);
    690         s.close();
     706        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    691707      }
    692708      HibernateUtil.commit(tx);
     
    731747      session = HibernateUtil.newSession();
    732748      tx = HibernateUtil.newTransaction(session);
    733       hasTable = tableExists(table.getTableName(db), HibernateUtil.getConnection(session));
     749      hasTable = tableExists(table.getTableName(db), session);
    734750      HibernateUtil.commit(tx);
    735751    }
     
    756772    Check if table in the dynamic database exists.
    757773    @param tableName The name of the table
    758     @param c The JDBC connection
     774    @param session The Hibernate session
    759775    @see #virtualTableExists(VirtualDb, VirtualTable)
    760776  */
    761   private static boolean tableExists(String tableName, Connection c)
     777  private static boolean tableExists(String tableName, Session session)
    762778    throws SQLException
    763779  {
    764     ResultSet tables = null;
    765     boolean hasTable = false;
    766     try
    767     {
    768       DatabaseMetaData metaData = c.getMetaData();
    769       tables = metaData.getTables(Application.getDynamicCatalog(),
    770         Application.getDynamicSchema(), tableName, null);
    771       hasTable = tables.next();
    772     }
    773     finally
    774     {
    775       if (tables != null) tables.close();
    776     }
    777     return hasTable;
     780    return HibernateUtil.doJdbcWork(session,
     781      new TableExistsWork(Application.getDynamicCatalog(), Application.getDynamicSchema(), tableName));
    778782  }
    779783 
     
    864868      // Important: we may get into problem with Keys otherwise which do their work
    865869      // in separate transactions
    866       HibernateUtil.getConnection(session).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
     870      HibernateUtil.doJdbcWork(session,
     871          new JdbcWork<Object>()
     872          {
     873            @Override
     874            public Object getResult()
     875            {
     876              return null;
     877            }
     878            @Override
     879            public void execute(Connection connection)
     880              throws SQLException
     881            {
     882              connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
     883            }
     884          }
     885      );
    867886      return session;
    868887    }
     
    912931  /**
    913932    Get the underlying JDBC connection from the Hibernate session.
    914     TODO - Hibernate 3.3 will probably have a replacement for
    915     the Session.connection() method. Update this method and other
    916     code that is using it. See
     933    Hibernate 3.3 have implemented a partial replacement for the
     934    Session.connection() method that can be used if the connection
     935    or any derived object such as a PreparedStatement or ResultSet is
     936    not needed after the connection has been used. Such code should
     937    use {@link #doJdbcWork(Session, JdbcWork)} instead.
     938    <p>
     939    Other code may continue using this method until the Hibernate
     940    team develops a full replacement.
     941    See
    917942    <a href="http://forum.hibernate.org/viewtopic.php?t=974518">
    918943    http://forum.hibernate.org/viewtopic.php?t=974518</a>
    919944    @since 2.4
     945    @see #doJdbcWork(Session, JdbcWork)
    920946  */
    921947  @SuppressWarnings("deprecation")
     
    930956      throw new BaseException(ex);
    931957    }
     958  }
     959 
     960  /**
     961    Execute some arbitrary JDBC code using the same
     962    database connection as the Hibernate session. This method
     963    should be used instead of {@link #getConnection(Session)}
     964    wherever possible.
     965
     966    @param session The Hibernate session to use
     967    @param work The work implementation
     968    @return Determined by the work implementation
     969    @throws SQLException In case there is an
     970      SQL error
     971    @since 2.9
     972  */
     973  static <R> R doJdbcWork(Session session, JdbcWork<R> work)
     974    throws SQLException
     975  {
     976    try
     977    {
     978      session.doWork(work);
     979    }
     980    catch (HibernateException ex)
     981    {
     982      if (ex.getCause() instanceof SQLException)
     983      {
     984        throw (SQLException)ex.getCause();
     985      }
     986      throw ex;
     987    }
     988    return work.getResult();
    932989  }
    933990 
     
    17391796    @param dropIndexes If true, all indexes will be dropped
    17401797  */
    1741   public static void dynamicDbIndexes(boolean verbose, boolean silent, boolean dropIndexes)
    1742   {
    1743     if (silent) verbose = false;
     1798  public static void dynamicDbIndexes(boolean verbose, final boolean silent, final boolean dropIndexes)
     1799  {
     1800    final boolean isVerbose = verbose && !silent;
    17441801    Session session = newSession();
    1745     Connection connection = HibernateUtil.getConnection(session);
    1746     DatabaseMetaData metaData = null;
    1747     List<Table> tables = new LinkedList<Table>();
    17481802   
    17491803    try
    17501804    {
    1751       metaData = connection.getMetaData();
    1752       ResultSet result = metaData.getTables(Application.getDynamicCatalog(),
    1753           Application.getDynamicSchema(), null, null);
    1754      
    1755       while (result.next())
    1756       {
    1757         String tableName = result.getString("TABLE_NAME");
    1758         String tableCatalog = result.getString("TABLE_CAT");
    1759         String tableSchema = result.getString("TABLE_SCHEM");
    1760         Table table = new Table();
    1761         table.setName(tableName);
    1762         table.setCatalog(tableCatalog);
    1763         table.setSchema(tableSchema);
    1764         tables.add(table);
    1765       }
    1766     }
    1767     catch (SQLException ex)
    1768     {
    1769       throw new BaseException(ex);
    1770     }
    1771    
    1772     for (Table table : tables)
    1773     {
    1774 
    1775       if (!silent)
    1776       {
    1777         System.out.println("=================");
    1778         System.out.println("Table   : " + table.getName());
    1779       }
    1780       if (verbose)
    1781       {
    1782         System.out.println("Catalog : " + table.getCatalog());
    1783         System.out.println("Schema  : " + table.getSchema());
    1784       }
    1785 
    1786       Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
    1787       Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
    1788      
    1789       String pkName = null;
    1790       Set<String> fkNames = new HashSet<String>();
    1791 
    1792       if (verbose)
    1793       {
    1794         System.out.println("Database information");
    1795         System.out.println("--------------------");
    1796       }
    1797      
    1798       TableInfo tiDb = null;
    1799       try
    1800       {
    1801         tiDb = new TableInfo(table, metaData);
    1802       }
    1803       catch (SQLException ex)
    1804       {
    1805         throw new BaseException(ex);
    1806       }
    1807 
    1808       if (tiDb != null)
    1809       {
    1810         if (verbose)
     1805      HibernateUtil.doJdbcWork(session,
     1806        new JdbcWork<Object>()
    18111807        {
    1812           // Write all columns
    1813           for (ColumnInfo ci : tiDb.getColumns())
     1808          @Override
     1809          public Object getResult()
    18141810          {
    1815             System.out.println("  Column       : " + ci);
     1811            return null;
    18161812          }
    1817          
    1818           // Write primary key
    1819           System.out.println("  Primary key  : " + tiDb.getPrimaryKey());
    1820        
    1821           // Write foreign keys
    1822           for (ForeignKeyInfo fk : tiDb.getForeignKeys())
     1813          @Override
     1814          public void execute(Connection connection)
     1815            throws SQLException
    18231816          {
    1824             System.out.println("  Foreign key  : " + fk);
    1825           }
    1826         }
    1827      
    1828         // Write indexes and unique constraints
    1829         for (IndexInfo ii : tiDb.getIndexes())
    1830         {
    1831           if (!silent)
    1832           {
    1833             if (ii.isUnique())
     1817            DatabaseMetaData metaData = null;
     1818            List<Table> tables = new LinkedList<Table>();
     1819           
     1820            metaData = connection.getMetaData();
     1821            ResultSet result = metaData.getTables(Application.getDynamicCatalog(),
     1822                Application.getDynamicSchema(), null, null);
     1823           
     1824            while (result.next())
    18341825            {
    1835               System.out.println("  Unique       : " + ii);
     1826              String tableName = result.getString("TABLE_NAME");
     1827              String tableCatalog = result.getString("TABLE_CAT");
     1828              String tableSchema = result.getString("TABLE_SCHEM");
     1829              Table table = new Table();
     1830              table.setName(tableName);
     1831              table.setCatalog(tableCatalog);
     1832              table.setSchema(tableSchema);
     1833              tables.add(table);
    18361834            }
    1837             else
     1835           
     1836            for (Table table : tables)
    18381837            {
    1839               System.out.println("  Index        : " + ii);
    1840             }
    1841           }
    1842          
    1843           boolean safeToDrop = tiDb.safeToDrop(ii);
    1844 
    1845           String dropSql = dbEngine.getDropIndexSql(
    1846             table.getCatalog(), table.getSchema(), table.getName(),
    1847             ii.getName(), ii.isUnique());
    1848           boolean actionDrop = dropIndexes && safeToDrop;
    1849 
    1850           if (!silent)
    1851           {
    1852             System.out.println("    Safe drop  : " + safeToDrop);
    1853             System.out.println("    DROP-SQL   : " + dropSql);
    1854             System.out.println("    Actions    : " + (actionDrop ? "DROP " : ""));
    1855           }
    1856          
    1857           if (actionDrop )
    1858           {
    1859             Statement st = null;
    1860             try
    1861             {
    1862               st = connection.createStatement();
    1863               if (actionDrop)
     1838 
     1839              if (!silent)
    18641840              {
    1865                 log.info("Dropping index: + " + dropSql);
    1866                 st.executeUpdate(dropSql);
     1841                System.out.println("=================");
     1842                System.out.println("Table   : " + table.getName());
    18671843              }
    1868               connection.commit();
    1869               st.close();
    1870               st = null;
    1871             }
    1872             catch (SQLException ex)
    1873             {
    1874               log.error("Exception", ex);
    1875               throw new BaseException(ex);
    1876             }
    1877             finally
    1878             {
    1879               if (st != null)
     1844              if (isVerbose)
    18801845              {
    1881                 try
     1846                System.out.println("Catalog : " + table.getCatalog());
     1847                System.out.println("Schema  : " + table.getSchema());
     1848              }
     1849 
     1850              Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
     1851              Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
     1852             
     1853              String pkName = null;
     1854              Set<String> fkNames = new HashSet<String>();
     1855 
     1856              if (isVerbose)
     1857              {
     1858                System.out.println("Database information");
     1859                System.out.println("--------------------");
     1860              }
     1861             
     1862              TableInfo tiDb = null;
     1863              try
     1864              {
     1865                tiDb = new TableInfo(table, metaData);
     1866              }
     1867              catch (SQLException ex)
     1868              {
     1869                throw new BaseException(ex);
     1870              }
     1871 
     1872              if (tiDb != null)
     1873              {
     1874                if (isVerbose)
    18821875                {
    1883                   st.close();
     1876                  // Write all columns
     1877                  for (ColumnInfo ci : tiDb.getColumns())
     1878                  {
     1879                    System.out.println("  Column       : " + ci);
     1880                  }
     1881                 
     1882                  // Write primary key
     1883                  System.out.println("  Primary key  : " + tiDb.getPrimaryKey());
     1884               
     1885                  // Write foreign keys
     1886                  for (ForeignKeyInfo fk : tiDb.getForeignKeys())
     1887                  {
     1888                    System.out.println("  Foreign key  : " + fk);
     1889                  }
    18841890                }
    1885                 catch (Throwable t)
     1891             
     1892                // Write indexes and unique constraints
     1893                for (IndexInfo ii : tiDb.getIndexes())
    18861894                {
    1887                   log.error("Exception", t);
     1895                  if (!silent)
     1896                  {
     1897                    if (ii.isUnique())
     1898                    {
     1899                      System.out.println("  Unique       : " + ii);
     1900                    }
     1901                    else
     1902                    {
     1903                      System.out.println("  Index        : " + ii);
     1904                    }
     1905                  }
     1906                 
     1907                  boolean safeToDrop = tiDb.safeToDrop(ii);
     1908 
     1909                  String dropSql = dbEngine.getDropIndexSql(
     1910                    table.getCatalog(), table.getSchema(), table.getName(),
     1911                    ii.getName(), ii.isUnique());
     1912                  boolean actionDrop = dropIndexes && safeToDrop;
     1913 
     1914                  if (!silent)
     1915                  {
     1916                    System.out.println("    Safe drop  : " + safeToDrop);
     1917                    System.out.println("    DROP-SQL   : " + dropSql);
     1918                    System.out.println("    Actions    : " + (actionDrop ? "DROP " : ""));
     1919                  }
     1920                 
     1921                  if (actionDrop )
     1922                  {
     1923                    Statement st = null;
     1924                    try
     1925                    {
     1926                      st = connection.createStatement();
     1927                      if (actionDrop)
     1928                      {
     1929                        log.info("Dropping index: + " + dropSql);
     1930                        st.executeUpdate(dropSql);
     1931                      }
     1932                      connection.commit();
     1933                      st.close();
     1934                      st = null;
     1935                    }
     1936                    catch (SQLException ex)
     1937                    {
     1938                      log.error("Exception", ex);
     1939                      throw new BaseException(ex);
     1940                    }
     1941                    finally
     1942                    {
     1943                      if (st != null)
     1944                      {
     1945                        try
     1946                        {
     1947                          st.close();
     1948                        }
     1949                        catch (Throwable t)
     1950                        {
     1951                          log.error("Exception", t);
     1952                        }
     1953                      }
     1954                    }
     1955                  }
     1956                 
    18881957                }
     1958              }
     1959             
     1960              if (!silent)
     1961              {
     1962                System.out.println("=================");
     1963                System.out.println("");
    18891964              }
    18901965            }
    18911966          }
    1892          
    18931967        }
    1894       }
    1895      
    1896       if (!silent)
    1897       {
    1898         System.out.println("=================");
    1899         System.out.println("");
    1900       }
     1968      );
     1969    }
     1970    catch (SQLException ex)
     1971    {
     1972      throw new BaseException(ex);
    19011973    }
    19021974  }
     
    19151987  */
    19161988  @SuppressWarnings("unchecked")
    1917   public static void dbIndexes(boolean verbose, boolean silent, boolean dropIndexes, boolean updateIndexes)
    1918   {
    1919     if (silent) verbose = false;
    1920     Iterator<Table> tables = (Iterator<Table>)cfg.getTableMappings();
     1989  public static void dbIndexes(boolean verbose, final boolean silent,
     1990    final boolean dropIndexes, final boolean updateIndexes)
     1991  {
     1992    final boolean isVerbose = verbose && !silent;
    19211993    Session session = newSession();
    1922     Connection connection = HibernateUtil.getConnection(session);
    1923     DatabaseMetaData metaData = null;
    19241994   
    19251995    try
    19261996    {
    1927       metaData = connection.getMetaData();
    1928     }
    1929     catch (SQLException ex)
    1930     {
    1931       throw new BaseException(ex);
    1932     }
    1933    
    1934     while (tables.hasNext())
    1935     {
    1936       Table table = tables.next();
    1937       if (!silent)
    1938       {
    1939         System.out.println("=================");
    1940         System.out.println("Table   : " + table.getName());
    1941       }
    1942       if (verbose)
    1943       {
    1944         System.out.println("Catalog : " + table.getCatalog());
    1945         System.out.println("Schema  : " + table.getSchema());
    1946       }
    1947  
    1948       Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
    1949       Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
    1950      
    1951       String pkName = null;
    1952       Set<String> fkNames = new HashSet<String>();
    1953  
    1954       if (verbose)
    1955       {
    1956         System.out.println("Database information");
    1957         System.out.println("--------------------");
    1958       }
    1959      
    1960       TableInfo tiDb = null;
    1961       try
    1962       {
    1963         tiDb = new TableInfo(table, metaData);
    1964       }
    1965       catch (SQLException ex)
    1966       {
    1967         throw new BaseException(ex);
    1968       }
    1969  
    1970       if (verbose && tiDb != null)
    1971       {
    1972         // Write all columns
    1973         for (ColumnInfo ci : tiDb.getColumns())
     1997      HibernateUtil.doJdbcWork(session,
     1998        new JdbcWork<Object>()
    19741999        {
    1975           System.out.println("  Column       : " + ci);
    1976         }
    1977        
    1978         // Write primary key
    1979         System.out.println("  Primary key  : " + tiDb.getPrimaryKey());
    1980      
    1981         // Write foreign keys
    1982         for (ForeignKeyInfo fk : tiDb.getForeignKeys())
    1983         {
    1984           System.out.println("  Foreign key  : " + fk);
    1985         }
    1986      
    1987         // Write indexes and unique constraints
    1988         for (IndexInfo ii : tiDb.getIndexes())
    1989         {
    1990           if (ii.isUnique())
     2000          @Override
     2001          public Object getResult()
    19912002          {
    1992             System.out.println("  Unique       : " + ii);
     2003            return null;
    19932004          }
    1994           else
     2005 
     2006          @Override
     2007          public void execute(Connection connection)
     2008            throws SQLException
    19952009          {
    1996             System.out.println("  Index        : " + ii);
    1997           }
    1998         }
    1999       }
    2000      
    2001       if (verbose)
    2002       {
    2003         System.out.println("Hibernate information");
    2004         System.out.println("---------------------");
    2005       }
    2006      
    2007       TableInfo tiHib = new TableInfo(table, dialect);
    2008      
    2009       if (verbose)
    2010       {
    2011         // Write all columns
    2012         for (ColumnInfo ci : tiHib.getColumns())
    2013         {
    2014           System.out.println("  Column       : " + ci);
    2015         }
    2016        
    2017         // Write primary key
    2018         System.out.println("  Primary key  : " + tiHib.getPrimaryKey());
    2019      
    2020         // Write foreign keys
    2021         for (ForeignKeyInfo fk : tiHib.getForeignKeys())
    2022         {
    2023           System.out.println("  Foreign key  : " + fk);
    2024         }
    2025       }
    2026  
    2027       // Write indexes and unique constraints
    2028       for (IndexInfo ii : tiHib.getIndexes())
    2029       {
    2030         if (!silent)
    2031         {
    2032           if (ii.isUnique())
    2033           {
    2034             System.out.println("  Unique       : " + ii);
    2035           }
    2036           else
    2037           {
    2038             System.out.println("  Index        : " + ii);
    2039           }
    2040         }
     2010            Iterator<Table> tables = (Iterator<Table>)cfg.getTableMappings();
     2011            DatabaseMetaData metaData = connection.getMetaData();
     2012            while (tables.hasNext())
     2013            {
     2014              Table table = tables.next();
     2015              if (!silent)
     2016              {
     2017                System.out.println("=================");
     2018                System.out.println("Table   : " + table.getName());
     2019              }
     2020              if (isVerbose)
     2021              {
     2022                System.out.println("Catalog : " + table.getCatalog());
     2023                System.out.println("Schema  : " + table.getSchema());
     2024              }
    20412025         
    2042         String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns());
    2043         boolean exists = dbName != null;
    2044         boolean safeToDrop = exists && tiDb.safeToDrop(ii);
    2045  
    2046         String dropSql = exists ?
    2047           dbEngine.getDropIndexSql(table.getCatalog(), table.getSchema(),
    2048             table.getName(), dbName, ii.isUnique()) : "";
    2049         String createSql =
    2050           dbEngine.getCreateIndexSql(table.getCatalog(), table.getSchema(),
    2051             table.getName(), table.getName() + "_" + ii.getName(),
    2052           ii.getColumns(), ii.isUnique());
    2053         boolean actionDrop = dropIndexes && exists && safeToDrop;
    2054         boolean actionCreate = updateIndexes && (actionDrop || !exists);
    2055        
    2056         if (!silent)
    2057         {
    2058           System.out.println("    Exists     : " + exists + (exists ? "(" + dbName + ")" : ""));
    2059           System.out.println("    Safe drop  : " + safeToDrop);
    2060           System.out.println("    DROP-SQL   : " + dropSql);
    2061           System.out.println("    CREATE-SQL : " + createSql);
    2062           System.out.println("    Actions    : " + (actionDrop ? "DROP " : "") +
    2063             (actionCreate ? "CREATE" : ""));
    2064         }
    2065         if (actionDrop || actionCreate)
    2066         {
    2067           Statement st = null;
    2068           try
    2069           {
    2070             st = connection.createStatement();
    2071             if (actionDrop)
    2072             {
    2073               log.info("Dropping index: + " + dropSql);
    2074               st.executeUpdate(dropSql);
    2075             }
    2076             if (actionCreate)
    2077             {
    2078               log.info("Creating index: + " + createSql);
    2079               st.executeUpdate(createSql);
    2080             }
    2081             connection.commit();
    2082             st.close();
    2083             st = null;
    2084           }
    2085           catch (SQLException ex)
    2086           {
    2087             log.error("Exception", ex);
    2088             throw new BaseException(ex);
    2089           }
    2090           finally
    2091           {
    2092             if (st != null)
    2093             {
     2026              Map<Set<String>, String> indexedColumns = new HashMap<Set<String>, String>();
     2027              Map<Set<String>, String> uniqueColumns = new HashMap<Set<String>, String>();
     2028             
     2029              String pkName = null;
     2030              Set<String> fkNames = new HashSet<String>();
     2031         
     2032              if (isVerbose)
     2033              {
     2034                System.out.println("Database information");
     2035                System.out.println("--------------------");
     2036              }
     2037             
     2038              TableInfo tiDb = null;
    20942039              try
    20952040              {
    2096                 st.close();
     2041                tiDb = new TableInfo(table, metaData);
    20972042              }
    2098               catch (Throwable t)
     2043              catch (SQLException ex)
    20992044              {
    2100                 log.error("Exception", t);
     2045                throw new BaseException(ex);
     2046              }
     2047         
     2048              if (isVerbose && tiDb != null)
     2049              {
     2050                // Write all columns
     2051                for (ColumnInfo ci : tiDb.getColumns())
     2052                {
     2053                  System.out.println("  Column       : " + ci);
     2054                }
     2055               
     2056                // Write primary key
     2057                System.out.println("  Primary key  : " + tiDb.getPrimaryKey());
     2058             
     2059                // Write foreign keys
     2060                for (ForeignKeyInfo fk : tiDb.getForeignKeys())
     2061                {
     2062                  System.out.println("  Foreign key  : " + fk);
     2063                }
     2064             
     2065                // Write indexes and unique constraints
     2066                for (IndexInfo ii : tiDb.getIndexes())
     2067                {
     2068                  if (ii.isUnique())
     2069                  {
     2070                    System.out.println("  Unique       : " + ii);
     2071                  }
     2072                  else
     2073                  {
     2074                    System.out.println("  Index        : " + ii);
     2075                  }
     2076                }
     2077              }
     2078             
     2079              if (isVerbose)
     2080              {
     2081                System.out.println("Hibernate information");
     2082                System.out.println("---------------------");
     2083              }
     2084             
     2085              TableInfo tiHib = new TableInfo(table, dialect);
     2086             
     2087              if (isVerbose)
     2088              {
     2089                // Write all columns
     2090                for (ColumnInfo ci : tiHib.getColumns())
     2091                {
     2092                  System.out.println("  Column       : " + ci);
     2093                }
     2094               
     2095                // Write primary key
     2096                System.out.println("  Primary key  : " + tiHib.getPrimaryKey());
     2097             
     2098                // Write foreign keys
     2099                for (ForeignKeyInfo fk : tiHib.getForeignKeys())
     2100                {
     2101                  System.out.println("  Foreign key  : " + fk);
     2102                }
     2103              }
     2104         
     2105              // Write indexes and unique constraints
     2106              for (IndexInfo ii : tiHib.getIndexes())
     2107              {
     2108                if (!silent)
     2109                {
     2110                  if (ii.isUnique())
     2111                  {
     2112                    System.out.println("  Unique       : " + ii);
     2113                  }
     2114                  else
     2115                  {
     2116                    System.out.println("  Index        : " + ii);
     2117                  }
     2118                }
     2119                 
     2120                String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns());
     2121                boolean exists = dbName != null;
     2122                boolean safeToDrop = exists && tiDb.safeToDrop(ii);
     2123         
     2124                String dropSql = exists ?
     2125                  dbEngine.getDropIndexSql(table.getCatalog(), table.getSchema(),
     2126                    table.getName(), dbName, ii.isUnique()) : "";
     2127                String createSql =
     2128                  dbEngine.getCreateIndexSql(table.getCatalog(), table.getSchema(),
     2129                    table.getName(), table.getName() + "_" + ii.getName(),
     2130                  ii.getColumns(), ii.isUnique());
     2131                boolean actionDrop = dropIndexes && exists && safeToDrop;
     2132                boolean actionCreate = updateIndexes && (actionDrop || !exists);
     2133               
     2134                if (!silent)
     2135                {
     2136                  System.out.println("    Exists     : " + exists + (exists ? "(" + dbName + ")" : ""));
     2137                  System.out.println("    Safe drop  : " + safeToDrop);
     2138                  System.out.println("    DROP-SQL   : " + dropSql);
     2139                  System.out.println("    CREATE-SQL : " + createSql);
     2140                  System.out.println("    Actions    : " + (actionDrop ? "DROP " : "") +
     2141                    (actionCreate ? "CREATE" : ""));
     2142                }
     2143                if (actionDrop || actionCreate)
     2144                {
     2145                  Statement st = null;
     2146                  try
     2147                  {
     2148                    st = connection.createStatement();
     2149                    if (actionDrop)
     2150                    {
     2151                      log.info("Dropping index: + " + dropSql);
     2152                      st.executeUpdate(dropSql);
     2153                    }
     2154                    if (actionCreate)
     2155                    {
     2156                      log.info("Creating index: + " + createSql);
     2157                      st.executeUpdate(createSql);
     2158                    }
     2159                    connection.commit();
     2160                    st.close();
     2161                    st = null;
     2162                  }
     2163                  catch (SQLException ex)
     2164                  {
     2165                    log.error("Exception", ex);
     2166                    throw new BaseException(ex);
     2167                  }
     2168                  finally
     2169                  {
     2170                    if (st != null)
     2171                    {
     2172                      try
     2173                      {
     2174                        st.close();
     2175                      }
     2176                      catch (Throwable t)
     2177                      {
     2178                        log.error("Exception", t);
     2179                      }
     2180                    }
     2181                  }
     2182                }
     2183               
     2184              }
     2185             
     2186              if (!silent)
     2187              {
     2188                System.out.println("=================");
     2189                System.out.println("");
    21012190              }
    21022191            }
     2192           
    21032193          }
     2194       
    21042195        }
    2105        
    2106       }
     2196      );
     2197    }
     2198    catch (SQLException ex)
     2199    {
    21072200     
    2108       if (!silent)
    2109       {
    2110         System.out.println("=================");
    2111         System.out.println("");
    2112       }
    2113     }
     2201    }
     2202   
    21142203  }
    21152204
  • trunk/src/core/net/sf/basedb/core/MappingBatcher.java

    r4479 r4503  
    2727
    2828import net.sf.basedb.core.data.RawData;
     29import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2930import net.sf.basedb.core.query.Expressions;
    3031import net.sf.basedb.core.query.Selects;
    31 import net.sf.basedb.core.signal.SignalException;
    3232
    3333import java.util.List;
     
    227227    @throws BaseException If there is an error
    228228  */
    229   public int insert(AbstractSqlQuery query)
     229  public int insert(final AbstractSqlQuery query)
    230230    throws BaseException
    231231  {
     
    246246   
    247247    // Holds the names of the parameters in the query in the order they appear
    248     List<String> parameterOrder = new LinkedList<String>();
     248    final List<String> parameterOrder = new LinkedList<String>();
    249249   
    250250    // Parse out named parameters
     
    255255    try
    256256    {
    257       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    258       query.setParameters(ps, parameterOrder);
    259       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    260       ps.close();
     257      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     258        new ExecuteUpdateWork(getSessionControl(), insertSql)
     259        {
     260          @Override
     261          public void setParameters(PreparedStatement ps)
     262            throws SQLException
     263          {
     264            query.setParameters(ps, parameterOrder);
     265          }
     266        }
     267      );
    261268      bytes += rowsInserted * bytesPerRow;
    262269      totalInsertCount += rowsInserted;
    263     }
    264     catch (InterruptedException ex)
    265     {
    266       throw new SignalException("Aborted by user.", ex);
    267270    }
    268271    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/PositionBatcher.java

    r4479 r4503  
    2727
    2828import net.sf.basedb.core.data.ReporterData;
     29import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2930import net.sf.basedb.core.query.Expressions;
    3031import net.sf.basedb.core.query.Selects;
    31 import net.sf.basedb.core.signal.SignalException;
    3232
    3333import java.util.List;
     
    223223    @throws BaseException If there is an error
    224224  */
    225   public int insert(AbstractSqlQuery query)
     225  public int insert(final AbstractSqlQuery query)
    226226    throws BaseException
    227227  {
     
    242242   
    243243    // Holds the names of the parameters in the query in the order they appear
    244     List<String> parameterOrder = new LinkedList<String>();
     244    final List<String> parameterOrder = new LinkedList<String>();
    245245   
    246246    // Parse out named parameters
     
    251251    try
    252252    {
    253       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    254       query.setParameters(ps, parameterOrder);
    255       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    256       ps.close();
     253      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     254        new ExecuteUpdateWork(getSessionControl(), insertSql)
     255        {
     256          @Override
     257          public void setParameters(PreparedStatement ps)
     258            throws SQLException
     259          {
     260            query.setParameters(ps, parameterOrder);
     261          }
     262        }
     263      );
    257264      bytes += rowsInserted * bytesPerRow;
    258265      totalInsertCount += rowsInserted;
    259     }
    260     catch (InterruptedException ex)
    261     {
    262       throw new SignalException("Aborted by user.", ex);
    263266    }
    264267    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/PositionExtraValueBatcher.java

    r4479 r4503  
    2626package net.sf.basedb.core;
    2727
     28import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2829import net.sf.basedb.core.query.Expressions;
    2930import net.sf.basedb.core.query.Selects;
    30 import net.sf.basedb.core.signal.SignalException;
    3131
    3232import java.util.List;
     
    317317    @throws BaseException If there is an error
    318318  */
    319   public int insert(AbstractSqlQuery query)
     319  public int insert(final AbstractSqlQuery query)
    320320    throws BaseException
    321321  {
     
    349349   
    350350    // Holds the names of the parameters in the query in the order they appear
    351     List<String> parameterOrder = new LinkedList<String>();
     351    final List<String> parameterOrder = new LinkedList<String>();
    352352   
    353353    // Parse out named parameters
     
    358358    try
    359359    {
    360       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    361       query.setParameters(ps, parameterOrder);
    362       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    363       ps.close();
     360      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     361          new ExecuteUpdateWork(getSessionControl(), insertSql)
     362          {
     363            @Override
     364            public void setParameters(PreparedStatement ps)
     365              throws SQLException
     366            {
     367              query.setParameters(ps, parameterOrder);
     368            }
     369          }
     370        );
    364371      bytes += rowsInserted * bytesPerRow;
    365372      totalInsertCount += rowsInserted;
    366     }
    367     catch (InterruptedException ex)
    368     {
    369       throw new SignalException("Aborted by user.", ex);
    370373    }
    371374    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/QueryExecutor.java

    r4479 r4503  
    5959  @base.modified $Date: 2007-08-17 09:18:29 +0200 (fr, 17 aug 2007) $
    6060*/
    61 class QueryExecutor
     61public class QueryExecutor
    6262{
    6363
     
    8888    @throws InterruptedException If this thread was interrupted
    8989  */
    90   static ResultSet executeQuery(PreparedStatement ps, SessionControl sc)
     90  public static ResultSet executeQuery(PreparedStatement ps, SessionControl sc)
    9191    throws InterruptedException
    9292  {
     
    102102    @throws InterruptedException If this thread was interrupted
    103103  */
    104   static int executeUpdate(PreparedStatement ps, SessionControl sc)
     104  public static int executeUpdate(PreparedStatement ps, SessionControl sc)
    105105    throws InterruptedException
    106106  {
     
    119119    @since 2.7
    120120  */
    121   static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query,
     121  public static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query,
    122122    StatelessSession session, SessionControl sc)
    123123    throws InterruptedException
     
    139139    @since 2.7
    140140  */
    141   static <T> T loadData(Class<T> clazz, Query query,
     141  public static <T> T loadData(Class<T> clazz, Query query,
    142142    StatelessSession session, SessionControl sc)
    143143    throws InterruptedException
  • trunk/src/core/net/sf/basedb/core/RawDataBatcher.java

    r4479 r4503  
    3131import net.sf.basedb.core.data.FeatureData;
    3232import net.sf.basedb.core.data.ReporterData;
     33import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    3334import net.sf.basedb.core.signal.SignalException;
    3435import net.sf.basedb.core.signal.ThreadSignalHandler;
     
    3940import java.util.Map;
    4041import java.util.Set;
    41 import java.sql.Statement;
    4242import java.sql.SQLException;
    4343
     
    634634    {
    635635      if (debugSqlEnabled) logSql.debug("Deleting raw data: " + sql);
    636       Statement s = HibernateUtil.getConnection(getDbControl().getHibernateSession()).createStatement();
    637       s.executeUpdate(sql);
     636      HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     637        new ExecuteUpdateWork(getSessionControl(), sql)
     638      );
    638639      rawBioAssayData.setSpots(0);
    639640      rawBioAssayData.setBytes(0);
  • trunk/src/core/net/sf/basedb/core/SpotBatcher.java

    r4479 r4503  
    2626package net.sf.basedb.core;
    2727
     28import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2829import net.sf.basedb.core.query.Expressions;
    2930import net.sf.basedb.core.query.Selects;
    30 import net.sf.basedb.core.signal.SignalException;
    3131
    3232import java.util.List;
     
    304304    @throws BaseException If there is an error
    305305  */
    306   public int insert(AbstractSqlQuery query)
     306  public int insert(final AbstractSqlQuery query)
    307307    throws BaseException
    308308  {
     
    330330   
    331331    // Holds the names of the parameters in the query in the order they appear
    332     List<String> parameterOrder = new LinkedList<String>();
     332    final List<String> parameterOrder = new LinkedList<String>();
    333333   
    334334    // Parse out named parameters
     
    339339    try
    340340    {
    341       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    342       query.setParameters(ps, parameterOrder);
    343       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    344       ps.close();
     341      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     342        new ExecuteUpdateWork(getSessionControl(), insertSql)
     343        {
     344          @Override
     345          public void setParameters(PreparedStatement ps)
     346            throws SQLException
     347          {
     348            query.setParameters(ps, parameterOrder);
     349          }
     350        }
     351      );
    345352      bytes += rowsInserted * bytesPerRow;
    346353      totalInsertCount += rowsInserted;
    347     }
    348     catch (InterruptedException ex)
    349     {
    350       throw new SignalException("Aborted by user.", ex);
    351354    }
    352355    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/SpotExtraValueBatcher.java

    r4479 r4503  
    2626package net.sf.basedb.core;
    2727
     28import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
    2829import net.sf.basedb.core.query.Expressions;
    2930import net.sf.basedb.core.query.Selects;
    30 import net.sf.basedb.core.signal.SignalException;
    3131
    3232import java.util.List;
     
    306306    @throws BaseException If there is an error
    307307  */
    308   public int insert(AbstractSqlQuery query)
     308  public int insert(final AbstractSqlQuery query)
    309309    throws BaseException
    310310  {
     
    338338   
    339339    // Holds the names of the parameters in the query in the order they appear
    340     List<String> parameterOrder = new LinkedList<String>();
     340    final List<String> parameterOrder = new LinkedList<String>();
    341341   
    342342    // Parse out named parameters
     
    347347    try
    348348    {
    349       PreparedStatement ps =  HibernateUtil.getConnection(getDbControl().getHibernateSession()).prepareStatement(insertSql);
    350       query.setParameters(ps, parameterOrder);
    351       rowsInserted = QueryExecutor.executeUpdate(ps, getSessionControl());
    352       ps.close();
     349      rowsInserted = HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     350        new ExecuteUpdateWork(getSessionControl(), insertSql)
     351        {
     352          @Override
     353          public void setParameters(PreparedStatement ps)
     354            throws SQLException
     355          {
     356            query.setParameters(ps, parameterOrder);
     357          }
     358        }
     359      );
    353360      bytes += rowsInserted * bytesPerRow;
    354361      totalInsertCount += rowsInserted;
    355     }
    356     catch (InterruptedException ex)
    357     {
    358       throw new SignalException("Aborted by user.", ex);
    359362    }
    360363    catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/Update.java

    r4479 r4503  
    2525package net.sf.basedb.core;
    2626
    27 import java.sql.Connection;
    28 import java.sql.DatabaseMetaData;
    2927import java.sql.SQLException;
    30 import java.sql.Statement;
    3128import java.util.Arrays;
    3229import java.util.Collections;
     
    6057import net.sf.basedb.core.dbengine.DbEngine;
    6158import net.sf.basedb.core.dbengine.TableInfo;
     59import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
     60import net.sf.basedb.core.hibernate.TableInfoWork;
    6261
    6362/**
     
    11411140    try
    11421141    {
    1143       Connection connection = HibernateUtil.getConnection(session);
    1144       DatabaseMetaData metaData = null;
    11451142      TableInfo helpInfo = null;
    11461143      try
    11471144      {
    1148         metaData = connection.getMetaData();
    1149         Table helpTable = new Table("HelpTexts");
    1150         helpInfo = new TableInfo(helpTable, metaData);
     1145        helpInfo = HibernateUtil.doJdbcWork(session,
     1146          new TableInfoWork(new Table("HelpTexts")));
    11511147      }
    11521148      catch (SQLException ex)
     
    11631159        try
    11641160        {
    1165           Statement st = connection.createStatement();
    11661161          log.info("Dropping index: " + sql);
    1167           st.executeUpdate(sql);
    1168           st.close();
     1162          HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    11691163        }
    11701164        catch (SQLException ex)
     
    13221316     
    13231317      // Copy existing parameters
    1324       Connection c = HibernateUtil.getConnection(session);
    13251318      try
    13261319      {
    1327         Statement s = c.createStatement();
    13281320        String sql = PredefinedQuery.getQueryString("COPY_PLUGINCONFIGURATION_PARAMETERS");
    13291321        /*
     
    13331325          FROM `PluginConfigurationValues`
    13341326        */
    1335         s.executeUpdate(sql);
     1327        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    13361328     
    13371329        sql = PredefinedQuery.getQueryString("DELETE_UNVERSIONED_PLUGINCONFIGURATION_PARAMETERS");
     
    13391331          DELETE FROM `PluginConfigurationValues`
    13401332        */
    1341         s.executeUpdate(sql);
     1333        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    13421334      }
    13431335      catch (SQLException ex)
     
    13771369    {
    13781370      tx = HibernateUtil.newTransaction(session);
    1379 
    1380       Connection c = HibernateUtil.getConnection(session);
    13811371      try
    13821372      {
    1383         Statement s = c.createStatement();
    1384        
    13851373        // Change discriminator for file values
    13861374        String sql = PredefinedQuery.getQueryString("CHANGE_FILE_DISCRIMINATOR");
     
    13901378          WHERE `discriminator` = 9
    13911379        */
    1392         s.executeUpdate(sql);
     1380        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    13931381     
    13941382        sql = PredefinedQuery.getQueryString("COPY_FILE_PARAMETERS");
     
    13981386          FROM `FileValues`
    13991387        */
    1400         s.executeUpdate(sql);
     1388        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    14011389
    14021390        // Delete old file values
     
    14051393          DELETE FROM `FileValues`
    14061394        */
    1407         s.executeUpdate(sql);
     1395        HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    14081396      }
    14091397      catch (SQLException ex)
     
    22042192    try
    22052193    {
    2206       Connection connection = HibernateUtil.getConnection(session);
    2207       DatabaseMetaData metaData = null;
    22082194      TableInfo info = null;
    22092195      try
    22102196      {
    2211         metaData = connection.getMetaData();
    2212         Table table = new Table("Features");
    2213         info = new TableInfo(table, metaData);
     2197        info = HibernateUtil.doJdbcWork(session,
     2198          new TableInfoWork(new Table("Features")));
    22142199      }
    22152200      catch (SQLException ex)
     
    22272212        try
    22282213        {
    2229           Statement st = connection.createStatement();
    22302214          log.info("Dropping index: " + sql);
    2231           st.executeUpdate(sql);
    2232           st.close();
     2215          HibernateUtil.doJdbcWork(session, new ExecuteUpdateWork(null, sql));
    22332216        }
    22342217        catch (SQLException ex)
  • trunk/src/core/net/sf/basedb/core/VirtualDb.java

    r4479 r4503  
    2828import net.sf.basedb.core.data.VirtualDbData;
    2929import net.sf.basedb.core.data.SharedData;
    30 import net.sf.basedb.core.signal.SignalException;
    31 
    32 import java.sql.Connection;
    33 import java.sql.PreparedStatement;
     30import net.sf.basedb.core.hibernate.ExecuteUpdateWork;
     31
    3432import java.sql.SQLException;
    3533
     
    331329    throws BaseException
    332330  {
    333     Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());
    334331    int rowsDeleted = 0;
    335332    for (VirtualTable table : VirtualTable.values())
     
    339336        String tableName = table.getQualifiedTableName(this);
    340337        String sql = "DELETE FROM " + tableName + " WHERE "+
    341           HibernateUtil.quote("cube")+" = ?";
    342         PreparedStatement delete = null;
     338          HibernateUtil.quote("cube")+" = " + cubeNo;
    343339        try
    344340        {
    345341          if (debugSqlEnabled) logSql.debug(sql);
    346           delete = c.prepareStatement(sql);
    347           delete.setShort(1, cubeNo);
    348           rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl());
    349         }
    350         catch (InterruptedException ex)
    351         {
    352           throw new SignalException("Aborted by user.", ex);
     342          rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     343              new ExecuteUpdateWork(getSessionControl(), sql));
    353344        }
    354345        catch (SQLException ex)
     
    357348          throw new BaseException(ex);
    358349        }
    359         finally
    360         {
    361           try
    362           {
    363             if (delete != null) delete.close();
    364           }
    365           catch (SQLException ex)
    366           {}
    367         }
    368350      }
    369351    }
     
    383365  {
    384366    int rowsDeleted = 0;
    385     Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());
    386367    if (HibernateUtil.virtualTableExists(this, VirtualTable.SPOT))
    387368    {
    388369      String tableName = VirtualTable.SPOT.getQualifiedTableName(this);
    389370      String sql = "DELETE FROM " + tableName + " WHERE "+
    390         HibernateUtil.quote("cube")+" = ? AND "+
    391         HibernateUtil.quote("layer")+" = ?";
    392       PreparedStatement delete = null;
     371        HibernateUtil.quote("cube")+" = " + cubeNo + " AND "+
     372        HibernateUtil.quote("layer")+" = " + layerNo;
    393373      try
    394374      {
    395375        if (debugSqlEnabled) logSql.debug(sql);
    396         delete = c.prepareStatement(sql);
    397         delete.setShort(1, cubeNo);
    398         delete.setShort(2, layerNo);
    399         rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl());
    400       }
    401       catch (InterruptedException ex)
    402       {
    403         throw new SignalException("Aborted by user.", ex);
     376        rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     377            new ExecuteUpdateWork(getSessionControl(), sql));
    404378      }
    405379      catch (SQLException ex)
     
    407381        logSql.error(sql, ex);
    408382        throw new BaseException(ex);
    409       }
    410       finally
    411       {
    412         try
    413         {
    414           if (delete != null) delete.close();
    415         }
    416         catch (SQLException ex)
    417         {}
    418383      }
    419384    }
     
    433398  {
    434399    int rowsDeleted = 0;
    435     Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());
    436400    if (HibernateUtil.virtualTableExists(this, VirtualTable.FILTER))
    437401    {
    438402      String tableName = VirtualTable.FILTER.getQualifiedTableName(this);
    439403      String sql = "DELETE FROM " + tableName + " WHERE "+
    440         HibernateUtil.quote("cube")+" = ? AND "+
    441         HibernateUtil.quote("filter")+" = ?";
    442       PreparedStatement delete = null;
     404        HibernateUtil.quote("cube")+" = " + cubeNo + " AND "+
     405        HibernateUtil.quote("filter")+" = " + filterNo;
     406     
    443407      try
    444408      {
    445409        if (debugSqlEnabled) logSql.debug(sql);
    446         delete = c.prepareStatement(sql);
    447         delete.setShort(1, cubeNo);
    448         delete.setShort(2, filterNo);
    449         rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl());
    450       }
    451       catch (InterruptedException ex)
    452       {
    453         throw new SignalException("Aborted by user.", ex);
     410        rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     411            new ExecuteUpdateWork(getSessionControl(), sql));
    454412      }
    455413      catch (SQLException ex)
     
    457415        logSql.error(sql, ex);
    458416        throw new BaseException(ex);
    459       }
    460       finally
    461       {
    462         try
    463         {
    464           if (delete != null) delete.close();
    465         }
    466         catch (SQLException ex)
    467         {}
    468417      }
    469418    }
     
    484433  {
    485434    int rowsDeleted = 0;
    486     Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());
     435    //Connection c = HibernateUtil.getConnection(getDbControl().getHibernateSession());
    487436    if (HibernateUtil.virtualTableExists(this, extraTable))
    488437    {
    489438      String tableName = extraTable.getQualifiedTableName(this);
    490       String sql = "DELETE FROM " + tableName + " WHERE "+
    491         HibernateUtil.quote("cube")+" = ? AND "+
    492         HibernateUtil.quote("extra")+" = ?";
    493       PreparedStatement delete = null;
     439      final String sql = "DELETE FROM " + tableName + " WHERE "+
     440        HibernateUtil.quote("cube")+" = " + cubeNo + " AND " +
     441        HibernateUtil.quote("extra")+" = " + extraNo;
     442     
    494443      try
    495444      {
    496445        if (debugSqlEnabled) logSql.debug(sql);
    497         delete = c.prepareStatement(sql);
    498         delete.setShort(1, cubeNo);
    499         delete.setShort(2, extraNo);
    500         rowsDeleted += QueryExecutor.executeUpdate(delete, getSessionControl());
    501       }
    502       catch (InterruptedException ex)
    503       {
    504         throw new SignalException("Aborted by user.", ex);
     446        rowsDeleted += HibernateUtil.doJdbcWork(getDbControl().getHibernateSession(),
     447            new ExecuteUpdateWork(getSessionControl(), sql));
    505448      }
    506449      catch (SQLException ex)
     
    509452        throw new BaseException(ex);
    510453      }
    511       finally
    512       {
    513         try
    514         {
    515           if (delete != null) delete.close();
    516         }
    517         catch (SQLException ex)
    518         {}
    519       }
    520454    }
    521455    return rowsDeleted;
Note: See TracChangeset for help on using the changeset viewer.