示例#1
0
/**
 * _gda_jdbc_make_error
 *
 * warning: STEALS @sql_state and @error.
 * Create a new #GdaConnectionEvent object and "adds" it to @cnc
 *
 * Returns: a new GdaConnectionEvent which must not be unrefed()
 */
GdaConnectionEvent *
_gda_jdbc_make_error (GdaConnection *cnc, gint error_code, gchar *sql_state, GError *error)
{
	GdaConnectionEvent *error_ev;
        GdaConnectionEventCode gda_code = GDA_CONNECTION_EVENT_CODE_UNKNOWN;
        GdaTransactionStatus *trans;

        error_ev = GDA_CONNECTION_EVENT (g_object_new (GDA_TYPE_CONNECTION_EVENT, "type",
						       (gint) GDA_CONNECTION_EVENT_ERROR, NULL));
	if (error) {
		gda_connection_event_set_description (error_ev,
						      error->message ? error->message : _("No detail"));
		g_error_free (error);
	}
	gda_connection_event_set_sqlstate (error_ev, sql_state);
	g_free (sql_state);
	gda_connection_event_set_code (error_ev, error_code);	
	gda_connection_event_set_gda_code (error_ev, gda_code);
        gda_connection_event_set_source (error_ev, "gda-jdbc");
        gda_connection_add_event (cnc, error_ev);

        /* change the transaction status if there is a problem */
        trans = gda_connection_get_transaction_status (cnc);
        if (trans) {
		/*
                if ((PQtransactionStatus (pconn) == PQTRANS_INERROR) &&
                    (trans->state != GDA_TRANSACTION_STATUS_STATE_FAILED))
                        gda_connection_internal_change_transaction_state (cnc,
                                                                          GDA_TRANSACTION_STATUS_STATE_FAILED);
		*/
        }
        return error_ev;
}
示例#2
0
/**
 * midgard_connection_reopen:
 * @self: #MidgardConnection instance
 * 
 * This is MySQL optimized workaround for lost connection event.
 *
 * Returns: %TRUE on success, %FALSE otherwise
 */
gboolean midgard_connection_reopen(MidgardConnection *self)
{
	g_assert(self != NULL);

	const gchar *query = "SELECT id FROM person LIMIT 1";
	GdaDataModel *model =
		midgard_core_query_get_model(self, query);

	if (model) {

		g_object_unref(model);
		return TRUE;
	}

	guint dbtype = self->priv->config->priv->dbtype;
	const GList *events;
	glong errcode = 0;
	events = gda_connection_get_events (self->priv->connection);

	if (!events) {
		
		g_warning("Can not get connection events. Error code is unknown.");
		return FALSE;
	}

	GList *l;
	for (l = (GList *) events; l != NULL; l = l->next) {

		GdaConnectionEvent *event;
		event = GDA_CONNECTION_EVENT (l->data);

		if (gda_connection_event_get_event_type (event) == GDA_CONNECTION_EVENT_ERROR) {

			errcode = gda_connection_event_get_code(event);

			switch (dbtype) {

				case MIDGARD_DB_TYPE_MYSQL:

					if (errcode == 2006) { /* CR_SERVER_GONE_ERROR , we can not use it */
						g_debug("MySQL server has gone away. Reconnect.");
						return __mysql_reconnect(self);
					}
					break;

				default:
					/* do nothing */
					break;
			}
		}
	}

	return FALSE;
}