Changeset 7947


Ignore:
Timestamp:
May 7, 2021, 1:44:50 PM (7 months ago)
Author:
Nicklas Nordborg
Message:

References #2250: Named transactions

Implemented code for updating existing change history entries. Several side-effects due to new parameter in ScrollIterator constructor.

Location:
trunk/src
Files:
11 edited

Legend:

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

    r7946 r7947  
    160160  {
    161161    this.sc = sc;
    162     System.out.println("Transaction name: "+name);
     162//    System.out.println("Transaction name: "+name);
    163163    this.name = name;
    164164   
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r7853 r7947  
    21272127    @since 3.12
    21282128  */
    2129   static <T> ScrollIterator<T> loadIterator(Query<T> query, SessionControl sc)
     2129  static <T> ScrollIterator<T> loadIterator(Query<T> query, boolean returnArray, SessionControl sc)
    21302130    throws BaseException
    21312131  {
     
    21362136      if (sc != null) pinger = Application.newPinger(sc);
    21372137      ScrollableResults result = query.scroll(ScrollMode.FORWARD_ONLY);
    2138       return new ScrollIterator<T>(result);
     2138      return new ScrollIterator<T>(result, returnArray);
    21392139    }
    21402140    catch (Throwable ex)
  • trunk/src/core/net/sf/basedb/core/Install.java

    r7899 r7947  
    119119    method.
    120120  */
    121   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(150).intValue();
     121  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(151).intValue();
    122122 
    123123  public static synchronized int createTables(SchemaGenerator.Mode mode, ProgressReporter progress,
     
    235235        roleJobAgent, quota0, true, true);
    236236 
    237       // Now that we have a root user let's create a session
    238       sessionControl = Application.newSessionControl( null, null, null );
     237      ClientData install = createClient(rootUser, "net.sf.basedb.core.install", "Install", "This is the installation program", null);
     238
     239      // Now that we have a root user and install client and let's create a session
     240      sessionControl = Application.newSessionControl(install.getExternalId(), null, null);
    239241      LoginRequest loginRequest = new LoginRequest(rootLogin, rootPassword);
    240242      loginRequest.setComment("InitDBSessionId");
     
    24722474    {
    24732475     
    2474       dc = sessionControl.newDbControl();
     2476      dc = sessionControl.newDbControl("Install: Create plug-in definitions");
    24752477      plugin = PluginDefinition.installOrUpdate(dc, info, null, shareToEveryone);
    24762478      if (plugin != null)
     
    25202522    try
    25212523    {
    2522       dc = sessionControl.newDbControl();
     2524      dc = sessionControl.newDbControl("Install: Create plug-in configurations");
    25232525     
    25242526      org.hibernate.query.Query<PluginConfigurationData> query = null;
  • trunk/src/core/net/sf/basedb/core/ItemQuery.java

    r7381 r7947  
    123123
    124124    ScrollIterator<? extends BasicData> result =
    125       HibernateUtil.loadIterator(getMainHqlQuery(dc, dataClass), sc);
     125      HibernateUtil.loadIterator(getMainHqlQuery(dc, dataClass), false, sc);
    126126    disableFilters(dc);
    127127    return new ItemResultIterator<I>(result, dc, itemClass, totalCount);
  • trunk/src/core/net/sf/basedb/core/QueryExecutor.java

    r7461 r7947  
    315315    {
    316316      ScrollableResults result = query.scroll(ScrollMode.FORWARD_ONLY);
    317       return new ScrollIterator<T>(result);
     317      return new ScrollIterator<T>(result, false);
    318318    }
    319319  }
  • trunk/src/core/net/sf/basedb/core/ScrollIterator.java

    r7551 r7947  
    4040
    4141  private final ScrollableResults results;
     42  private final boolean returnArray;
    4243  private boolean isClosed;
    4344  private E nextElement;
    4445
    45   ScrollIterator(ScrollableResults results)
     46  ScrollIterator(ScrollableResults results, boolean returnArray)
    4647  {
    4748    assert results != null : "results == null";
    4849    this.results = results;
     50    this.returnArray = returnArray;
    4951    this.isClosed = false;
    5052    this.nextElement = null;
     
    6567        if (results.next())
    6668        {
    67           nextElement = (E)results.get(0);
     69          nextElement = returnArray ? (E)results.get() : (E)results.get(0);
    6870        }
    6971        else
  • trunk/src/core/net/sf/basedb/core/StringUtil.java

    r6022 r7947  
    291291    return value.substring(0, 1).toLowerCase() + value.substring(1);
    292292  }
     293 
     294  /**
     295    Return the first non-null value (or null if all values are null).
     296    @since 3.19
     297  */
     298  public static final String coalesce(String... values)
     299  {
     300    if (values == null || values.length == 0) return null;
     301    for (String v : values)
     302    {
     303      if (v != null) return v;
     304    }
     305    return null;
     306  }
     307 
    293308}
  • trunk/src/core/net/sf/basedb/core/Update.java

    r7926 r7947  
    4747import net.sf.basedb.core.data.BioPlateData;
    4848import net.sf.basedb.core.data.BioPlateTypeData;
     49import net.sf.basedb.core.data.ChangeHistoryData;
    4950import net.sf.basedb.core.data.ChangeHistoryDetailData;
    5051import net.sf.basedb.core.data.ClientData;
     
    459460    </td>
    460461  </tr>
     462  <tr>
     463    <td>151</td>
     464    <td>
     465      Added {@link ChangeHistoryData#getName()}. The update will generate a default name for all
     466      existing entries based on the registred job, plug-in, client, etc.
     467    </td>
     468  </tr>
    461469  </table>
    462470
     
    755763      }
    756764   
     765      if (schemaVersion < 151)
     766      {
     767        if (progress != null) progress.display((int)(progress_current), "--Updating schema version: " + schemaVersion + " -> 151...");
     768        schemaVersion = updateToSchemaVersion151(session, schemaVersion, progress);
     769        progress_current += progress_step;
     770      }
     771
    757772      sc.logout();
    758773      if (progress != null) progress.display(100, "Database updated successfully.");
     
    958973
    959974      if (progress != null) progress.display(50, "Checking existing items...");
    960       // Test root user account
    961       SessionControl sc = Application.newSessionControl(null, null, null);
     975      // Test root user account (the install client was added in schema version 151 so we can't use this before updating)
     976      SessionControl sc = Application.newSessionControl(schemaVersion < 151 ? null : "net.sf.basedb.core.install", null, null);
    962977      LoginRequest loginRequest = new LoginRequest(rootLogin, rootPassword);
    963978      sc.login(loginRequest);
     
    14631478     
    14641479      org.hibernate.query.Query<ChangeHistoryDetailData> query = HibernateUtil.createQuery(statelessSession, hql, ChangeHistoryDetailData.class);
    1465       Iterator<ChangeHistoryDetailData> changes = HibernateUtil.loadIterator(query, null);
     1480      Iterator<ChangeHistoryDetailData> changes = HibernateUtil.loadIterator(query, false, null);
    14661481     
    14671482      int i = 0;
     
    21572172        "where [id] = :id");
    21582173     
    2159       Iterator<JobData> it = HibernateUtil.loadIterator(query, null);
     2174      Iterator<JobData> it = HibernateUtil.loadIterator(query, false, null);
    21602175      while (it.hasNext())
    21612176      {
     
    23212336  }
    23222337
     2338  /**
     2339   
     2340    @return The new schema version (=151)
     2341  */
     2342  private static int updateToSchemaVersion151(org.hibernate.Session session, int currentSchemaVersion, ProgressReporter progress)
     2343    throws BaseException
     2344  {
     2345    final int schemaVersion = 151;
     2346    org.hibernate.Transaction tx = null;
     2347   
     2348    try
     2349    {
     2350      tx = HibernateUtil.newTransaction(session);
     2351     
     2352      // Pre-load names for all clients, plug-ins and jobs
     2353      Map<Integer, String> clientNames = loadIdToNameMap(session, "select [c].[id], [c].[name] from [Clients] [c]");
     2354      Map<Integer, String> pluginNames = loadIdToNameMap(session, "select [p].[id], [p].[name] from [PluginDefinitions] [p]");
     2355      Map<Integer, String> jobNames = loadIdToNameMap(session, "select [j].[id], [j].[name] from [Jobs] [j]");
     2356      if (progress != null) progress.append(".");
     2357
     2358      // Count all
     2359      String countSql = "select count(*) from ChangeHistoryData ch where ch.name is null";
     2360      org.hibernate.query.Query<Long> countQuery = HibernateUtil.createQuery(session, countSql, Long.class);
     2361      long numItems = HibernateUtil.loadData(countQuery);
     2362      int delta = (int)numItems / 50;
     2363
     2364      // Load all ChangeHistory entries
     2365      String sql = "select [ch].[id], [ch].[client_id], [ch].[plugin_id], [ch].[job_id] "+
     2366          " from [ChangeHistory] [ch] where [ch].[name] is null";
     2367      org.hibernate.query.Query<Object[]> query = HibernateUtil.createSqlQuery(session, sql, Object[].class);
     2368     
     2369      // This query is used to update the 'name' for a single entry
     2370      org.hibernate.query.Query<?> fixQuery = HibernateUtil.createSqlQuery(session,
     2371        "update [ChangeHistory] set [name] = :name where [id] = :id");
     2372     
     2373      int count = 0;
     2374      Iterator<Object[]> it = HibernateUtil.loadIterator(query, true, null);
     2375      while (it.hasNext())
     2376      {
     2377        Object[] ch = it.next();
     2378       
     2379        int id = (Integer)ch[0];
     2380        Integer clientId = (Integer)ch[1];
     2381        Integer pluginId = (Integer)ch[2];
     2382        Integer jobId = (Integer)ch[3];
     2383        String clientName = clientId != null ? clientNames.get(clientId) : null;
     2384        String pluginName = pluginId != null ? pluginNames.get(pluginId) : null;
     2385        String jobName = jobId != null ? jobNames.get(jobId) : null;
     2386       
     2387        String name = "Unknown";
     2388        if (jobName != null && pluginName != null && !jobName.contains(pluginName))
     2389        {
     2390          name = StringUtil.trimString(pluginName+": "+jobName, Nameable.MAX_NAME_LENGTH);
     2391        }
     2392        else
     2393        {
     2394          name = StringUtil.coalesce(jobName, pluginName, clientName, "Unknown");
     2395        }
     2396       
     2397        fixQuery.setParameter("name", name, TypeWrapper.H_STRING);
     2398        fixQuery.setParameter("id", id, TypeWrapper.H_INTEGER);
     2399        fixQuery.executeUpdate();
     2400
     2401        count++;
     2402        if (progress != null && count % delta == 0) progress.append(".");
     2403      }
     2404
     2405      // Update the schema version number
     2406      setSchemaVersion(session, schemaVersion);
     2407     
     2408      // Commit the changes
     2409      HibernateUtil.commit(tx);
     2410      log.info("updateToSchemaVersion151: OK");
     2411    }
     2412    catch (RuntimeException ex)
     2413    {
     2414      if (tx != null) HibernateUtil.rollback(tx);
     2415      log.error("updateToSchemaVersion151: FAILED", ex);
     2416      throw ex;
     2417    }
     2418    return schemaVersion;
     2419  }
     2420
     2421 
     2422  private static Map<Integer, String> loadIdToNameMap(org.hibernate.Session session, String sql)
     2423  {
     2424    Map<Integer, String> map = new HashMap<>();
     2425    org.hibernate.query.Query<Object[]> query = HibernateUtil.createSqlQuery(session, sql, Object[].class);
     2426    Iterator<Object[]> it = HibernateUtil.loadIterator(query, true, null);
     2427    while (it.hasNext())
     2428    {
     2429      Object[] e = it.next();
     2430      map.put((Integer)e[0], (String)e[1]);
     2431    }
     2432    return map;
     2433  }
    23232434 
    23242435  /**
  • trunk/src/install/net/sf/basedb/install/OneTimeFix.java

    r7899 r7947  
    7777            .disableInternalJobQueue(true)
    7878            );
    79         SessionControl sc = Application.newSessionControl(null, null, null);
     79        SessionControl sc = Application.newSessionControl("net.sf.basedb.core.install", null, null);
    8080        sc.login(new LoginRequest(login, pwd));
    8181        int numModified = Update.recalculateRemainingQuantity(new ConsoleProgressReporter(false));
     
    104104            .disableInternalJobQueue(true)
    105105            );
    106         SessionControl sc = Application.newSessionControl(null, null, null);
     106        SessionControl sc = Application.newSessionControl("net.sf.basedb.core.install", null, null);
    107107        sc.login(new LoginRequest(login, pwd));
    108108        int numModified = Update.recalculateFreeWells(new ConsoleProgressReporter(false));
  • trunk/src/install/net/sf/basedb/install/Webclient.java

    r7899 r7947  
    9797        .disableInternalJobQueue(true)
    9898        );
    99     SessionControl sc = Application.newSessionControl(null, null, null);
     99    SessionControl sc = Application.newSessionControl("net.sf.basedb.core.install", null, null);
    100100    LoginRequest loginRequest = new LoginRequest(login, password);
    101101    loginRequest.setComment("Installing web client");
    102102    sc.login(loginRequest);
    103103   
    104     DbControl dc = sc.newDbControl();
     104    DbControl dc = sc.newDbControl("Install: Web client");
    105105    try
    106106    {
  • trunk/src/plugins/core/net/sf/basedb/plugins/HelpImporter.java

    r7714 r7947  
    331331    try
    332332    {
    333       dc = sc.newDbControl();       
     333      dc = sc.newDbControl(":Import help texts");
    334334      client = Client.getById(dc, client.getId());
    335335      float progLastUpdated = 0;
Note: See TracChangeset for help on using the changeset viewer.