diff --git a/include/asterisk/res_odbc.h b/include/asterisk/res_odbc.h
index 8bcd772b2fb42f56a863aa6a593a3ed443b6baa9..d90dbba9f908f0a83b6cf769363943afd017d9dc 100644
--- a/include/asterisk/res_odbc.h
+++ b/include/asterisk/res_odbc.h
@@ -40,6 +40,11 @@ struct odbc_obj {
 	SQLHDBC  con;                   /* ODBC Connection Handle */
 	struct odbc_class *parent;      /* Information about the connection is protected */
 	struct timeval last_used;
+#ifdef DEBUG_THREADS
+	char file[80];
+	char function[80];
+	int lineno;
+#endif
 	unsigned int used:1;
 	unsigned int up:1;
 	AST_LIST_ENTRY(odbc_obj) list;
@@ -100,7 +105,12 @@ int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) __attribute__ ((
  * thread which requests it.  Note that all connections should be released
  * when the thread is done by calling odbc_release_obj(), below.
  */
+#ifdef DEBUG_THREADS
+struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno);
+#define ast_odbc_request_obj(a, b)	_ast_odbc_request_obj(a, b, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#else
 struct odbc_obj *ast_odbc_request_obj(const char *name, int check);
+#endif
 
 /*! 
  * \brief Releases an ODBC object previously allocated by odbc_request_obj()
diff --git a/res/res_odbc.c b/res/res_odbc.c
index 2279096a93e72a94537ebf1f8ec4b5bf864c369f..674bc09e84d7ec4f4dc045391c56f51b2ba87e0c 100644
--- a/res/res_odbc.c
+++ b/res/res_odbc.c
@@ -143,6 +143,9 @@ struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *
 	if (tableptr) {
 		AST_RWLIST_RDLOCK(&tableptr->columns);
 		AST_RWLIST_UNLOCK(&odbc_tables);
+		if (obj) {
+			ast_odbc_release_obj(obj);
+		}
 		return tableptr;
 	}
 
@@ -579,9 +582,16 @@ static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_c
 
 				while ((current = ao2_iterator_next(&aoi2))) {
 					ast_mutex_lock(&current->lock);
+#ifdef DEBUG_THREADS
+					ast_cli(a->fd, "    - Connection %d: %s (%s:%d %s)\n", ++count,
+						current->used ? "in use" :
+						current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected",
+						current->file, current->lineno, current->function);
+#else
 					ast_cli(a->fd, "    - Connection %d: %s\n", ++count,
 						current->used ? "in use" :
 						current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
+#endif
 					ast_mutex_unlock(&current->lock);
 					ao2_ref(current, -1);
 				}
@@ -631,6 +641,11 @@ void ast_odbc_release_obj(struct odbc_obj *obj)
 	/* For pooled connections, this frees the connection to be
 	 * reused.  For non-pooled connections, it does nothing. */
 	obj->used = 0;
+#ifdef DEBUG_THREADS
+	obj->file[0] = '\0';
+	obj->function[0] = '\0';
+	obj->lineno = 0;
+#endif
 	ao2_ref(obj, -1);
 }
 
@@ -639,7 +654,11 @@ int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
 	return obj->parent->backslash_is_escape;
 }
 
+#ifdef DEBUG_THREADS
+struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
+#else
 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
+#endif
 {
 	struct odbc_obj *obj = NULL;
 	struct odbc_class *class;
@@ -721,6 +740,12 @@ struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
 	} else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_sec(ast_tvnow(), obj->last_used) > obj->parent->idlecheck)
 		odbc_obj_connect(obj);
 
+#if DEBUG_THREADS
+	ast_copy_string(obj->file, file, sizeof(obj->file));
+	ast_copy_string(obj->function, function, sizeof(obj->function));
+	obj->lineno = lineno;
+#endif
+
 	return obj;
 }