[libgda] Update the BLOB example to mention the transation started automatically



commit 7dbde6c7d5a126837613a134b91498efb99c0d18
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sat Feb 4 11:18:22 2012 +0100

    Update the BLOB example to mention the transation started automatically
    
    whenever a blob is read

 doc/C/examples/blobtest.c |   25 +++++++++++++++++++++++++
 samples/Blobs/README      |   16 ++++++++++++----
 samples/Blobs/blobtest.c  |   25 +++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 4 deletions(-)
---
diff --git a/doc/C/examples/blobtest.c b/doc/C/examples/blobtest.c
index e554758..b3c33aa 100644
--- a/doc/C/examples/blobtest.c
+++ b/doc/C/examples/blobtest.c
@@ -41,6 +41,9 @@ main (int argc, char *argv[])
 		result = do_store (cnc, filename, &error);
 	else
 		result = do_fetch (cnc, id, &error);
+
+	if (gda_connection_get_transaction_status (cnc))
+		g_print ("Still in a transaction, all modifications will be lost when connection is closed\n");
         gda_connection_close (cnc);
 
 	if (!result) {
@@ -112,11 +115,17 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 	blob = (GdaBlob*) gda_value_get_blob (value);
 	g_assert (gda_holder_take_value (holder, value, NULL));
 
+	g_print ("Before writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	g_print ("STORING file '%s' to database BLOB\n", filename);
 	res = gda_connection_statement_execute_non_select (cnc, stmt, params, &newrow, error);
 	g_object_unref (params);
 	g_object_unref (stmt);
 
+	g_print ("After writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	if (newrow) {
 		GSList *list;
 		g_print ("Inserted row is (for each numbered column in the table):\n");
@@ -133,6 +142,10 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 	else
 		g_print ("Provider did not return the inserted row\n");
 
+	gda_connection_rollback_transaction (cnc, NULL, NULL);
+	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	return (res == -1) ? FALSE : TRUE;
 }
 
@@ -147,6 +160,9 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 	GdaBlob *blob;
 	gboolean result = TRUE;
 
+	g_print ("Before reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	gchar *filename;
 	filename = g_strdup_printf ("fetched_%d", id);
 	g_print ("FETCHING BLOB with ID %d to file '%s'\n", id, filename);
@@ -170,6 +186,9 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 	if (! model)
 		return FALSE;
 
+	g_print ("After reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	value = gda_data_model_get_value_at (model, 0, 0, error);
 	if (!value) {
 		g_object_unref (model);
@@ -191,6 +210,12 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 		result = g_file_set_contents (filename, (gchar *) ((GdaBinary*)blob)->data,
 					     ((GdaBinary*)blob)->binary_length, error);
 	g_free (filename);
+	g_object_unref (model);
+
+	gda_connection_rollback_transaction (cnc, NULL, NULL);
+	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	return result;
 }
 ]]>
diff --git a/samples/Blobs/README b/samples/Blobs/README
index 3f82f8d..d144424 100644
--- a/samples/Blobs/README
+++ b/samples/Blobs/README
@@ -19,6 +19,9 @@ This test program offers 2 operations which are to store the contents of a file
 database (which returns the ID of the stored data), and to fetch a stored data from
 the database from its ID (which creates a fetched_<ID> file).
 
+This program also shows that transactions are created when reading BLOBs, and that these
+transactions need to be rolled back after the BLOB "handle" (data model and/or GValue) has been released.
+
 Compiling and running:
 ----------------------
 
@@ -27,15 +30,20 @@ To compile (make sure Libgda is installed prior to this):
 
 and to run (stores the blobtest executable file):
 > ./blobtest store blobtest
+Before writing BLOB: No transaction started
 STORING file 'blobtest' to database BLOB
+After writing BLOB: Transaction started
 Inserted row is (for each numbered column in the table):
   [+0] = [1]
-  [+1] = [\177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000p\215\004\0104\000\000\000\260/\000\000\000\000\000\000]
+  [+1] = [\177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000\020\216\004\0104\000\000\000\2542\000\000\000\000\000\000]
+After rolling back blob READ transaction: No transaction started
 Ok.
-> /blobtest fetch 1
+> ./blobtest fetch 1
+Before reading BLOB: No transaction started
 FETCHING BLOB with ID 1 to file 'fetched_1'
+After reading BLOB: Transaction started
+After rolling back blob READ transaction: No transaction started
 Ok.
-
 > cmp blobtest fetched_1
 
-Should not return any difference
\ No newline at end of file
+Should not return any difference
diff --git a/samples/Blobs/blobtest.c b/samples/Blobs/blobtest.c
index 08285a4..f33801e 100644
--- a/samples/Blobs/blobtest.c
+++ b/samples/Blobs/blobtest.c
@@ -54,6 +54,9 @@ main (int argc, char *argv[])
 		result = do_store (cnc, filename, &error);
 	else
 		result = do_fetch (cnc, id, &error);
+
+	if (gda_connection_get_transaction_status (cnc))
+		g_print ("Still in a transaction, all modifications will be lost when connection is closed\n");
         gda_connection_close (cnc);
 
 	if (!result) {
@@ -125,11 +128,17 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 	blob = (GdaBlob*) gda_value_get_blob (value);
 	g_assert (gda_holder_take_value (holder, value, NULL));
 
+	g_print ("Before writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	g_print ("STORING file '%s' to database BLOB\n", filename);
 	res = gda_connection_statement_execute_non_select (cnc, stmt, params, &newrow, error);
 	g_object_unref (params);
 	g_object_unref (stmt);
 
+	g_print ("After writing BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	if (newrow) {
 		GSList *list;
 		g_print ("Inserted row is (for each numbered column in the table):\n");
@@ -146,6 +155,10 @@ do_store (GdaConnection *cnc, const gchar *filename, GError **error)
 	else
 		g_print ("Provider did not return the inserted row\n");
 
+	gda_connection_rollback_transaction (cnc, NULL, NULL);
+	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	return (res == -1) ? FALSE : TRUE;
 }
 
@@ -160,6 +173,9 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 	GdaBlob *blob;
 	gboolean result = TRUE;
 
+	g_print ("Before reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	gchar *filename;
 	filename = g_strdup_printf ("fetched_%d", id);
 	g_print ("FETCHING BLOB with ID %d to file '%s'\n", id, filename);
@@ -183,6 +199,9 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 	if (! model)
 		return FALSE;
 
+	g_print ("After reading BLOB: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	value = gda_data_model_get_value_at (model, 0, 0, error);
 	if (!value) {
 		g_object_unref (model);
@@ -204,5 +223,11 @@ do_fetch (GdaConnection *cnc, gint id, GError **error)
 		result = g_file_set_contents (filename, (gchar *) ((GdaBinary*)blob)->data,
 					     ((GdaBinary*)blob)->binary_length, error);
 	g_free (filename);
+	g_object_unref (model);
+
+	gda_connection_rollback_transaction (cnc, NULL, NULL);
+	g_print ("After rolling back blob READ transaction: %s\n", gda_connection_get_transaction_status (cnc) ?
+		 "Transaction started" : "No transaction started");
+
 	return result;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]