Changeset 7946


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

References #2250: Named transactions

Added ChangeHistoryData.getName() method, SessionControl.newDbControl(String). Related changes in DbControl, LogControl, TransactionDetails and ChangeHistory so that the specified name is actually stored in the change log.

If no name is specified when creating a new DbControl a name is automatically generated by SessionControl.getDefaultTransactionName() based on the current job name, plug-in name and client name.

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

Legend:

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

    r7381 r7946  
    295295 
    296296  /**
     297    Get the name of the transaction that initiated the change.
     298    @since 3.19
     299  */
     300  public String getName()
     301  {
     302    return getData().getChangeHistory().getName();
     303  }
     304 
     305  /**
    297306    Get the type of change that was made.
    298307  */
  • trunk/src/core/net/sf/basedb/core/DbControl.java

    r7912 r7946  
    9090 
    9191  /**
     92    The name of this transaction.
     93    @since 3.19
     94  */
     95  private final String name;
     96 
     97  /**
    9298    The state is holding Hibernate database
    9399    connections and transaction.
     
    150156    Create a new object.
    151157  */
    152   DbControl(SessionControl sc)
     158  DbControl(SessionControl sc, String name)
    153159    throws BaseException
    154160  {
    155161    this.sc = sc;
     162    System.out.println("Transaction name: "+name);
     163    this.name = name;
    156164   
    157165    // Initialize logging extensions...
     
    196204  {
    197205    return sc;
     206  }
     207 
     208  /**
     209    Get the name of this transaction.
     210    @since 3.19
     211  */
     212  public String getName()
     213  {
     214    return name;
    198215  }
    199216 
  • trunk/src/core/net/sf/basedb/core/LogControl.java

    r7522 r7946  
    4545
    4646  private final WeakReference<DbControl> dc;
     47  private final String name;
    4748  private TransactionDetails transactionDetails;
    4849  private org.hibernate.StatelessSession session;
     
    5556  {
    5657    this.dc = new WeakReference<DbControl>(dc);
     58    this.name = dc.getName();
    5759    // Use a higher batch size here since we are dealing with small objects
    5860    this.batchSize = Config.getInt("db.batch-size", 50) * 10;
     61  }
     62 
     63  /**
     64    Get the name of this transaction.
     65    @since 3.19
     66  */
     67  public String getName()
     68  {
     69    return name;
    5970  }
    6071 
  • trunk/src/core/net/sf/basedb/core/SessionControl.java

    r7816 r7946  
    107107  */
    108108  private final int clientId;
     109  private final String clientName;
    109110 
    110111  /**
     
    165166 
    166167  private int pluginId;
     168  private String pluginName;
    167169 
    168170  private int jobId;
     171  private String jobName;
    169172 
    170173  /**
     
    182185    this.id = id;
    183186    this.clientId = parent.clientId;
     187    this.clientName = parent.clientName;
    184188    this.externalClientId = parent.externalClientId;
    185189    this.remoteId = parent.remoteId;
    186190    this.dbControlCache = Collections.synchronizedMap(new WeakHashMap<DbControl,String>());
    187191    this.currentContexts = parent.currentContexts;
    188     if (plugin != null) this.pluginId = plugin.getId();
    189     if (job != null) this.jobId = job.getId();
     192    if (plugin != null)
     193    {
     194      this.pluginId = plugin.getId();
     195      this.pluginName = plugin.getName();
     196    }
     197    if (job != null)
     198    {
     199      this.jobId = job.getId();
     200      this.jobName = job.getName();
     201    }
    190202    LoginInfo li = new LoginInfo(parent.loginInfo);
    191203    li.keyring = new Keyring(li.keyring, pluginId, plugin != null && plugin.getUsePermissions());
     
    212224    this.allowedClients = Collections.synchronizedMap(new HashMap<String, Boolean>());
    213225    int tempClientId = 0;
     226    String tempClientName = null;
    214227    if (externalClientId != null)
    215228    {
     
    234247        {
    235248          tempClientId = clientData.getId();
     249          tempClientName = clientData.getName();
    236250          // load default settings for client application
    237251          clientDefaultSettings = loadClientDefaultSettings(session, tempClientId);
     
    249263    }
    250264    this.clientId = tempClientId;
     265    this.clientName = tempClientName;
    251266    updateLastAccess();
    252267  }
     
    318333
    319334  /**
     335    Get the name of the <code>Client</code> application in use.
     336    @return The client name, or null if no client was specified
     337    @since 3.19
     338   */
     339  public String getClientName()
     340  {
     341    updateLastAccess();
     342    return clientName;
     343  }
     344 
     345  /**
    320346    Get the external id of the <code>Client</code> application in use.
    321347    @return A java.lang.String object.
     
    340366  }
    341367
    342  
     368  /**
     369    Get the name of the <code>UserDevice</code> in use.
     370    @return Device name as a String, or null if the device is unknown
     371    @since 3.19
     372  */
     373  public String getDeviceName()
     374  {
     375    updateLastAccess();
     376    return loginInfo == null ? null : loginInfo.deviceName;
     377  }
     378
    343379  /**
    344380    Get the remote id that was passed to the {@link
     
    381417    throws BaseException
    382418  {
     419    return newDbControl(null);
     420  }
     421 
     422  /**
     423    Create a new name <code>DbControl</code> object for this session.
     424    @param name The name of the transaction or null to generate a default name based on
     425      currently active plug-in/job/client etc. If the name starts with ':'
     426      {@link #getDefaultTransactionName(String)} is called with the name as the
     427      suffix.
     428    @return A <code>DbControl</code> object
     429    @throws BaseException If there is an error
     430    @since 3.19
     431  */
     432  public DbControl newDbControl(String name)
     433  {
    383434    updateLastAccess();
    384     DbControl dbControl = new DbControl(this);
     435    if (name == null || name.startsWith(":"))
     436    {
     437      name = getDefaultTransactionName(name);
     438    }
     439    DbControl dbControl = new DbControl(this, name);
    385440    dbControlCache.put(dbControl, null);
    386441    return dbControl;
     442  }
     443 
     444  /**
     445    Generate a default transaction name with an optional suffix. The
     446    name is selected in the following priority order:
     447   
     448    1. If a job is running the name of the job is used. If the job
     449       is a plug-in the name of the plug-in is also included (unless
     450       it is already part of the job name).
     451    2. If a plug-in is running without a job, the name of the plug-in
     452       is used together with the suffix.
     453    3. If a client is specified the name of the client is used
     454       together with the suffix.
     455    4. The suffix is used by itself
     456    5. The name is set to "Unknown".
     457   
     458    @since 3.19
     459  */
     460  public String getDefaultTransactionName(String suffix)
     461  {
     462    String name = null;
     463    if (suffix != null && suffix.startsWith(":")) suffix = suffix.substring(1).strip();
     464    if (pluginName != null && jobName != null && !jobName.contains(pluginName))
     465    {
     466      name = StringUtil.trimString(pluginName+": "+jobName, Nameable.MAX_NAME_LENGTH);
     467    }
     468    else if (jobName != null)
     469    {
     470      name = jobName;
     471    }
     472    else if (pluginName != null)
     473    {
     474      name = pluginName;
     475      if (suffix != null) name += ": " + suffix;
     476    }
     477    else if (clientName != null)
     478    {
     479      name = clientName;
     480      if (suffix != null) name += ": " + suffix;
     481    }
     482    else if (suffix != null)
     483    {
     484      name = suffix;
     485    }
     486    else
     487    {
     488      name = "Unknown";
     489    }
     490    return name;
    387491  }
    388492 
     
    10531157    {
    10541158      deviceData = HibernateUtil.loadData(session, UserDeviceData.class, deviceId);
     1159      li.deviceName = deviceData.getName();
    10551160    }
    10561161
     
    12341339 
    12351340  /**
     1341    Get the name of the plug-in that is currently running with this
     1342    session control.
     1343    @return The plug-in name, or null if not running by a plug-in
     1344    @since 3.19
     1345   */
     1346  public String getPluginName()
     1347  {
     1348    updateLastAccess();
     1349    return pluginName;
     1350  }
     1351 
     1352  /**
    12361353    Get the id of the job that is currently running with this
    12371354    session control.
     
    12441361    return jobId;
    12451362  }
     1363 
     1364  /**
     1365    Get the name of the job that is currently running with this
     1366    session control.
     1367    @return The job name, or null if not running by a job
     1368    @since 3.19
     1369   */
     1370  public String getJobName()
     1371  {
     1372    updateLastAccess();
     1373    return jobName;
     1374  }
     1375
    12461376 
    12471377  /**
     
    27262856    */
    27272857    private int deviceId;
     2858    private String deviceName;
    27282859   
    27292860    /**
     
    27802911      this.userName = parent.userName;
    27812912      this.deviceId = parent.deviceId;
     2913      this.deviceName = parent.deviceName;
    27822914      this.activeProjectId = parent.activeProjectId;
    27832915      this.activeProjectName = parent.activeProjectName;
  • trunk/src/core/net/sf/basedb/core/data/ChangeHistoryData.java

    r6351 r7946  
    7575  }
    7676
     77  private String name;
     78  /**
     79    Get the name of the transaction.
     80    @return A <code>String</code> with the name of the transaction
     81    @since 3.19
     82    @hibernate.property type="string"
     83    @hibernate.column name="`name`" length="255" not-null="true" index="name_idx"
     84  */
     85  public String getName()
     86  {
     87    return name;
     88  }
     89  public void setName(String name)
     90  {
     91    this.name = name;
     92  }
     93 
    7794  private int userId;
    7895  /**
  • trunk/src/core/net/sf/basedb/core/log/TransactionDetails.java

    r7381 r7946  
    125125    ChangeHistoryData history = new ChangeHistoryData();
    126126    history.setTime(new Date(System.currentTimeMillis()));
     127    history.setName(getName());
    127128    history.setUserId(userId);
    128129    history.setSessionId(sessionId);
     
    135136 
    136137  /**
     138    Get the name of this transaction.
     139    @since 3.19
     140  */
     141  public String getName()
     142  {
     143    return logControl.getName();
     144  }
     145 
     146  /**
    137147    Utility method for loading the name of the currently logged in user.
    138148    The first call to this method will lookup the name in the database
Note: See TracChangeset for help on using the changeset viewer.