You need to properly shut down the database connection when using automatic transactions (which is the default when $wgCommandLineMode is true). Add something like this at the end of your file:
$lb = wfGetLBFactory();
$lb->shutdown();
You'd also probably want to catch any exceptions that might be thrown and do a rollback on all open database connections. As for MediaWiki bug 56269, that's not your issue (although the lack of an ability to do a rollback on all open database connections is probably contributing to that bug).