/** Issue a non-SELECT query (ie: update/delete/insert) to the database.
 *
 */
static sql_rcode_t sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config, char const *query) {
	rlm_sql_firebird_conn_t *conn = handle->conn;

	int deadlock = 0;

#ifdef _PTHREAD_H
	pthread_mutex_lock(&conn->mut);
#endif

	try_again:
	/*
	 *	Try again query when deadlock, beacuse in any case it
	 *	will be retried.
	 */
 	if (fb_sql_query(conn, query)) {
		/* but may be lost for short sessions */
   		if ((conn->sql_code == DEADLOCK_SQL_CODE) &&
   		    !deadlock) {
	  		DEBUG("conn_id deadlock. Retry query %s", query);

			/*
			 *	@todo For non READ_COMMITED transactions put
			 *	rollback here
			 *	fb_rollback(conn);
			 */
	  		deadlock = 1;
	  		goto try_again;
	  	}

		ERROR("conn_id rlm_sql_firebird,sql_query error: sql_code=%li, error='%s', query=%s",
		      (long int) conn->sql_code, conn->error, query);

		if (conn->sql_code == DOWN_SQL_CODE) {
#ifdef _PTHREAD_H
			pthread_mutex_lock(&conn->mut);
#endif

			return RLM_SQL_RECONNECT;
		}

		/* Free problem query */
		if (fb_rollback(conn)) {
			//assume the network is down if rollback had failed
			ERROR("Fail to rollback transaction after previous error: %s", conn->error);

			return RLM_SQL_RECONNECT;
		}
		//   conn->in_use=0;
		return -1;
   	}

	if (conn->statement_type != isc_info_sql_stmt_select) {
		if (fb_commit(conn)) {
			return -1;
		}
	}

	return 0;
}
/** End the select query, such as freeing memory or result.
 *
 */
static sql_rcode_t sql_finish_select_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config)
{
	rlm_sql_firebird_conn_t *conn = (rlm_sql_firebird_conn_t *) handle->conn;

	fb_commit(conn);
	fb_close_cursor(conn);

	return 0;
}
/** End the select query, such as freeing memory or result.
 *
 */
static int sql_finish_select_query(rlm_sql_handle_t *handle,
				   UNUSED rlm_sql_config_t *config) {
				   
	rlm_sql_firebird_sock *sock = (rlm_sql_firebird_sock *) handle->conn;
	
	fb_commit(sock);
	fb_close_cursor(sock);
	
	return 0;
}
Exemple #4
0
static int sql_query(SQLSOCK *sqlsocket, SQL_CONFIG *config, char *querystr) {
    rlm_sql_firebird_sock *firebird_sock = sqlsocket->conn;
    int deadlock=0;
 if (config->sqltrace)
        radlog(L_DBG, "sock_id %i: query:  %s", sqlsocket->id,querystr);

#ifdef _PTHREAD_H
 pthread_mutex_lock(&firebird_sock->mut);
#endif

TryAgain:
 if (fb_sql_query(firebird_sock,querystr)) {
//Try again query when deadlock, beacuse in any case it will be retried.
// but may be lost for short sessions
   if ((firebird_sock->sql_code==DEADLOCK_SQL_CODE) && !deadlock) {
      radlog(L_DBG,"sock_id %i: deadlock. Retry query %s\n",sqlsocket->id,querystr);
//For non READ_COMMITED transactions put rollback here
// fb_rollback(sock);
      deadlock=1;
      goto TryAgain;
   }
   radlog(L_ERR, "sock_id %i: rlm_sql_firebird,sql_query error:sql_code=%li, error='%s', query=%s\n",
     sqlsocket->id,
     firebird_sock->sql_code,
     firebird_sock->lasterror,
     querystr);

   if ((firebird_sock->sql_code==DOWN_SQL_CODE)) return SQL_DOWN;
//free problem query
   if (fb_rollback(firebird_sock)) {
    //assume the network is down if rollback had failed
    radlog(L_ERR,"Fail to rollback transaction after previous error. Error: %s\n",
       firebird_sock->lasterror);
    return SQL_DOWN;
   }
//   firebird_sock->in_use=0;
   return -1;
 }

 if (firebird_sock->statement_type!=isc_info_sql_stmt_select) {
    if (fb_commit(firebird_sock)) return -1;
 }

 return 0;
}
static int sql_socket_destructor(void *c)
{
	rlm_sql_firebird_conn_t *conn = c;
	int i;
	
	DEBUG2("rlm_sql_firebird: socket destructor called, closing socket");
	
	fb_commit(conn);
	if (conn->dbh) {
		fb_free_statement(conn);
		mod_detach_database(conn->status, &(conn->dbh));
		
		if (fb_lasterror(conn)) {
			DEBUGW("rlm_sql_firebird: Got error "
			       "when closing socket: %s", conn->lasterror);
		}
	}
	
#ifdef _PTHREAD_H
	pthread_mutex_destroy (&conn->mut);
#endif

	for (i=0; i < conn->row_fcount; i++) {
		free(conn->row[i]);
	}
	
	free(conn->row);
	free(conn->row_sizes);
	fb_free_sqlda(conn->sqlda_out);
	
	free(conn->sqlda_out);
	free(conn->tpb);
	free(conn->dpb);
	
	if (conn->lasterror) {
		free(conn->lasterror);
	}

	return 0;
}
Exemple #6
0
/*************************************************************************
 *
 *	Function: sql_finish_select_query
 *
 *	Purpose: End the select query, such as freeing memory or result
 *
 *************************************************************************/
static int sql_finish_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
    rlm_sql_firebird_sock *sock=(rlm_sql_firebird_sock *) sqlsocket->conn;
    fb_commit(sock);
    fb_close_cursor(sock);
    return 0;
}
/** Issue a non-SELECT query (ie: update/delete/insert) to the database.
 *
 */
static int sql_query(rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t *config,
		     char *querystr) {
	rlm_sql_firebird_sock *firebird_sock = handle->conn;
	
	int deadlock = 0;

#ifdef _PTHREAD_H
	pthread_mutex_lock(&firebird_sock->mut);
#endif

	try_again:
	/* 
	 *	Try again query when deadlock, beacuse in any case it
	 *	will be retried.
	 */
 	if (fb_sql_query(firebird_sock,querystr)) {
		/* but may be lost for short sessions */
   		if ((firebird_sock->sql_code == DEADLOCK_SQL_CODE) &&
   		    !deadlock) {
	  		radlog(L_DBG,"sock_id deadlock. Retry query %s",
	  		       querystr);
	  		
			/*
			 *	@todo For non READ_COMMITED transactions put 
			 *	rollback here
			 *	fb_rollback(sock);
			 */
	  		deadlock = 1;
	  		goto try_again;
	  	}
  	
		radlog(L_ERR, "sock_id rlm_sql_firebird,sql_query error: "
		       "sql_code=%li, error='%s', query=%s",
		       (long int) firebird_sock->sql_code,
		       firebird_sock->lasterror,
		       querystr);

		if (firebird_sock->sql_code == DOWN_SQL_CODE) {
			return SQL_DOWN;
		}
	
		/* Free problem query */
		if (fb_rollback(firebird_sock)) {
			//assume the network is down if rollback had failed
			radlog(L_ERR,"Fail to rollback transaction after "
			       "previous error. Error: %s",
			       firebird_sock->lasterror);
		
			return SQL_DOWN;
		}
		//   firebird_sock->in_use=0;
		return -1;
   	}

	if (firebird_sock->statement_type != isc_info_sql_stmt_select) {
		if (fb_commit(firebird_sock)) {
			return -1;
		}
	}

	return 0;
}