attr-sql: Use a serializable transaction when inserting identities
This commit is contained in:
@@ -50,9 +50,8 @@ static u_int get_identity(private_sql_attribute_t *this, identification_t *id)
|
|||||||
{
|
{
|
||||||
enumerator_t *e;
|
enumerator_t *e;
|
||||||
u_int row;
|
u_int row;
|
||||||
int try = 0;
|
|
||||||
|
|
||||||
retry:
|
this->db->transaction(this->db, TRUE);
|
||||||
/* look for peer identity in the identities table */
|
/* look for peer identity in the identities table */
|
||||||
e = this->db->query(this->db,
|
e = this->db->query(this->db,
|
||||||
"SELECT id FROM identities WHERE type = ? AND data = ?",
|
"SELECT id FROM identities WHERE type = ? AND data = ?",
|
||||||
@@ -61,26 +60,20 @@ retry:
|
|||||||
if (e && e->enumerate(e, &row))
|
if (e && e->enumerate(e, &row))
|
||||||
{
|
{
|
||||||
e->destroy(e);
|
e->destroy(e);
|
||||||
|
this->db->commit(this->db);
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
DESTROY_IF(e);
|
DESTROY_IF(e);
|
||||||
if (try > 0)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
/* not found, insert new one */
|
/* not found, insert new one */
|
||||||
if (this->db->execute(this->db, &row,
|
if (this->db->execute(this->db, &row,
|
||||||
"INSERT INTO identities (type, data) VALUES (?, ?)",
|
"INSERT INTO identities (type, data) VALUES (?, ?)",
|
||||||
DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id)) == 1)
|
DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id)) == 1)
|
||||||
{
|
{
|
||||||
|
this->db->commit(this->db);
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
/* the INSERT could fail due to the UNIQUE constraint, if the identity was
|
this->db->rollback(this->db);
|
||||||
* added concurrently by another thread or the pool utility,
|
return 0;
|
||||||
* therefore try finding it again. a nicer fix would be to use locking
|
|
||||||
* on the database, but our API currently not supports that */
|
|
||||||
try++;
|
|
||||||
goto retry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user