Issues with ODBC queries failing

I have a a bunch of ODBC functions that I use all the time and for the most part they work well. At the beginning of the call I do a query to find out the owner of number. The query is

SELECT customers.id, customers.name, customers.email, dids.processor_id FROM customers JOIN dids ON customers.id = dids.customer_id WHERE dids.did = '18005551212'

In the logs I see

[2024-02-09 14:07:30] WARNING[1568622][C-000000cc] func_odbc.c: SQL Exec Direct failed (-1)![SELECT customers.id, customers.name, customers.email, dids.processor_id FROM customers JOIN dids ON customers.id = dids.customer_id WHERE dids.did = '18005551212']

If I hang up and call again a moment later it seems to work fine. What would cause Asterisk not to be able to execute a query? Below are my configurations,

/etc/odbc.ini

[asterisk-gg]
Description = MySQL connection to 'gg' database
Driver = MariaDB
Database = gg
Server = HOST
Port = 3306
Trace = yes
TraceFile=/tmp/odbc_trace.log

res_odbc.conf

[group_gift]
dsn => asterisk-gg
username => gg
password => PASSWORD
pre-connect => yes
sanitysql => select 1
max_connections => 300
forcecommit => no
connect_timeout => 5
negative_connection_cache => 300
logging => yes
slow_query_limit => 1000

func_odb.conf

[GG_GET_DID]
writehandle=gg
synopsis=Get the owner of the DID that is being called
readsql=SELECT customers.id, customers.name, customers.email, dids.processor_id FROM customers JOIN dids ON customers.id = dids.customer_id WHERE dids.did = '${SQL_ESC(${ARG1})}';

Dialplan

Exten => s,     1,              Noop()
 same =>        n,              Answer()
 same =>        n,              Set(DID_INFO=${ODBC_GG_GET_DID(${DID})})

It seems as if the connection is down and then next time I call back it works. What would cause this? I assume this ugly hack below would work but I prefer to fix the root cause of the issue.

Exten => s,     1,              Noop()
 same =>        n(ring),        Ringing()
 same =>        n,              Wait(1)
 same =>        n,              Set(DID_INFO=${ODBC_GG_GET_DID(${DID})})
 same =>        n,              GotoIf($["${ODBCSTATUS}" != "SUCCESS"]?ring)
 same =>        n,              Answer()

Any advice?

Are there any errors in your MariaDB logs ?

What about relevant output in /tmp/odbc_trace.log file ?

Curious if you see this immediately after an Asterisk restart ? Or only if the ODBC connection has idled-out for a while ?