tags:

views:

60

answers:

0

The MySQL UDF system doesn't provide a good way to have long-term persistent state.

I want to be able to write a function that takes the name of a server, and some other arguments, that connects to the server and sends a request, and returns the result. But connecting to the server is a heavyweight operation (much more than just opening a TCP socket); I'd like to cache open connections (with some kind of thread-safe connection pool).

The problem is knowing when I can free connections, as MySQL doesn't tell me anything useful such as when the session ends.

Sure, for every UDF, there are init and deinit functions - but they are only of any help when calling the same function for every row in a query (such as SELECT MyFunc(a) FROM table); init is called at the start of the query and deinit at the end.

I'm wondering if just registering an atexit() handler is the best way to go.