summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: adda7be)
raw | patch | inline | side by side (parent: adda7be)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 14 Nov 2012 20:11:36 +0000 (21:11 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 14 Nov 2012 20:11:36 +0000 (21:11 +0100) |
Also, make sure to acquire the database lock before that in order to wait for,
for example, outstanding writers.
for example, outstanding writers.
src/postgresql.c | patch | blob | history |
diff --git a/src/postgresql.c b/src/postgresql.c
index ff6fa86017a1fca62d1420806cffec9d27618537..4d9fc564ff5f143a9aa88fdc43f9ae023b9f38cd 100644 (file)
--- a/src/postgresql.c
+++ b/src/postgresql.c
static c_psql_writer_t *writers = NULL;
static size_t writers_num = 0;
+static int c_psql_begin (c_psql_database_t *db)
+{
+ PGresult *r = PQexec (db->conn, "BEGIN");
+
+ int status = 1;
+
+ if (r != NULL) {
+ if (PGRES_COMMAND_OK == PQresultStatus (r)) {
+ db->next_commit = cdtime() + db->commit_interval;
+ status = 0;
+ }
+ else
+ log_warn ("Failed to initiate ('BEGIN') transaction: %s",
+ PQerrorMessage (db->conn));
+ PQclear (r);
+ }
+ return status;
+} /* c_psql_begin */
+
+static int c_psql_commit (c_psql_database_t *db)
+{
+ PGresult *r = PQexec (db->conn, "COMMIT");
+
+ int status = 1;
+
+ if (r != NULL) {
+ if (PGRES_COMMAND_OK == PQresultStatus (r)) {
+ db->next_commit = cdtime () + db->commit_interval;
+ log_debug ("Successfully committed transaction.");
+ status = 0;
+ }
+ else
+ log_warn ("Failed to commit transaction: %s",
+ PQerrorMessage (db->conn));
+ PQclear (r);
+ }
+ return status;
+} /* c_psql_commit */
+
static c_psql_database_t *c_psql_database_new (const char *name)
{
c_psql_database_t *db;
c_psql_database_t *db = data;
+ /* wait for the lock to be released by the last writer */
+ pthread_mutex_lock (&db->db_lock);
+
+ if (db->next_commit > 0)
+ c_psql_commit (db);
+
PQfinish (db->conn);
db->conn = NULL;
sfree (db->writers);
db->writers_num = 0;
- /* wait for the lock to be released by the last writer */
- pthread_mutex_lock (&db->db_lock);
pthread_mutex_unlock (&db->db_lock);
pthread_mutex_destroy (&db->db_lock);
return string;
} /* values_to_sqlarray */
-static int c_psql_begin (c_psql_database_t *db)
-{
- PGresult *r = PQexec (db->conn, "BEGIN");
-
- int status = 1;
-
- if (r != NULL) {
- if (PGRES_COMMAND_OK == PQresultStatus (r)) {
- db->next_commit = cdtime() + db->commit_interval;
- status = 0;
- }
- else
- log_warn ("Failed to initiate ('BEGIN') transaction: %s",
- PQerrorMessage (db->conn));
- PQclear (r);
- }
- return status;
-} /* c_psql_begin */
-
-static int c_psql_commit (c_psql_database_t *db)
-{
- PGresult *r = PQexec (db->conn, "COMMIT");
-
- int status = 1;
-
- if (r != NULL) {
- if (PGRES_COMMAND_OK == PQresultStatus (r)) {
- db->next_commit = cdtime () + db->commit_interval;
- log_debug ("Successfully committed transaction.");
- status = 0;
- }
- else
- log_warn ("Failed to commit transaction: %s",
- PQerrorMessage (db->conn));
- PQclear (r);
- }
- return status;
-} /* c_psql_commit */
-
static int c_psql_write (const data_set_t *ds, const value_list_t *vl,
user_data_t *ud)
{