transaction_manager

Directory

eXtremeDB_rt/samples/transaction_manager

Description

Demonstrates the MUlti-Read SIngle-Write transaction manager

Used API

sample_start_connected_task(), sample_join_task(), mco_trans_start(), mco_trans_commit()

Code snippet

 
    void task1( sample_task_t * descriptor ) 
    {
      MCO_RET rc;
      mco_uint4 i;
    
      for (i = 0; i < n_iterations; i++) 
      { 
        mco_trans_h t;
        A a;
        char tag[64];
    
        rc = mco_trans_start(descriptor->db_connection, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); 
        if ( MCO_S_OK == rc ) {
          sprintf(tag, "Stream 1, rec %u\n", i);
          mem_write(tag);
          rc = A_new(t, &a);
          rc = A_ui4_put(&a, i);
          rc = A_tag_put(&a, tag, strlen(tag)); 
          sample_sleep(nap_duration1);
          rc = mco_trans_commit(t);
        }
      }
    }
    
    void task2( sample_task_t * descriptor ) 
    {
      MCO_RET rc;
      mco_uint4 i;
    
      for (i = 0; i < n_iterations; i++) 
      {
        mco_trans_h t;
        B b;
        char tag[64];
    
        rc = mco_trans_start(descriptor->db_connection, MCO_READ_WRITE, MCO_TRANS_FO
    REGROUND, &t); 
        if ( MCO_S_OK == rc ) {
          sprintf(tag, "Stream 2, rec %u\n", i);
          mem_write(tag);
          rc = B_new(t, &b);
          rc = B_ui4_put(&b, i);
          rc = B_tag_put(&b, tag, strlen(tag));   
          sample_sleep(nap_duration2);
          rc = mco_trans_commit(t);
        }
      }
    }
    
    
    
    rc = sample_open_database( db_name, transdb_get_dictionary(), DATABASE_SIZE, CACHE_SIZE, 
                                   MEMORY_PAGE_SIZE, PSTORAGE_PAGE_SIZE, 1 + N_THREADS, &dbmem );
      if ( MCO_S_OK == rc ) {
    
        sample_start_connected_task(&tasks[0], task1, db_name, 0);
        sample_start_connected_task(&tasks[1], task2, db_name, 0);
        sample_join_task(&tasks[0]);
        sample_join_task(&tasks[1]);
    
        sample_close_database(db_name, &dbmem);

What to expect as an output

 
	Two threads are started to run in parallel.  The first thread writes
	to record A and the second to record B.  As they write the database 
	records, output is written also to a memory buffer which is unspooled
	afterwards to show the actual order of operations.
	The thread interaction can be affected by three command line arguments:
	   n_iterations - number of records written to output (default=10) 
	   nap_duration1 - sleep time for task 1 (default=10 msec)
	   nap_duration2 - sleep time for task 2 (default=5 msec)
    
    Stream 1, rec 0
    Stream 2, rec 0
    Stream 1, rec 1
    Stream 2, rec 1