MYSQL for dialplan routing

I am looking to route incoming calls based on a mysql query, basically a call comes in and a mysql query of the dnis (number dialed) results in the trunk group / device to send (Dial) the call to. At the same time write cdr information to a different table. (I believe I can use a cdr_custom for that) I have found information on the possibility of that, but do I have to open and close the sql connection each call or can I open the connection and keep it open and just do the queries? Is there pro’s or con’s for doing it either way if I have a choice and would opening and closing each call cause a delay? (the sql server would run on the same PC). If I can just open it once how would I do it (global?) Any input would be appreciated! Thanks! - Jeremy

It depends on the module You’re using to connect to mysql (when queries are implemented in dialplan logic):

app_mysql.so -> The connection is channel-based e.g. needs to be opened and closed at least per channel, better per query

res_odbc.so/func_odbc.so -> The connection is global/shared and will be kept alive independent on channels (connection may be down depending on connection timeout parameters of the database but will be automatically re-opened by asterisk in case of a connection loss)

As app_mysql.so is deprecated (and IMHO even more complex to code) i would suggest to use the ODBC-connection. It even seems to be the faster and less resssource-intensive aproach.

For this type of application I’ve used visual dial plan in the past by Apstel. It has a database integration addon which allows MySQL queries to be performed very easily. It’s also pretty easy to use although it’s best to have some experience constructing dial plans manually to understand some of the things that you can do.