0

In PDO (And likewise DBAL) if there's a problem authenticating with the server it will throw an exception containing a trace including the database username and password.

Needless to say, this is a problem, and in PDO I would wrap it in a try block and re-throw the error without the stack trace. Problem solved.

However, DBAL doesn't actually initiate the connection until the first query is called, so it misses the try block completely and spews my database credentials the first chance it gets!

How can I force DBAL to connect immediately so I can catch any authentication errors?

J V
  • 11,402
  • 10
  • 52
  • 72

2 Answers2

0

\Doctrine\DBAL\Connection::connect()

Obvious in retrospect.

J V
  • 11,402
  • 10
  • 52
  • 72
0

My database is ibm db2 so I use the odbc pdo connection with dbal. This is how I set up my connection, notice that the PDO is in a try catch block

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;


$connPdo = null;
try {

    $connPdo = new PDO('odbc:'.SYSTEM_DSN_NAME, DB_USERNAME, DB_PASSWORD, array(
      PDO::ATTR_PERSISTENT => TRUE, 
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
    ); 

} catch (PDOException $e) {
    echo $e->getMessage() . '</br>';
}

$config = new Configuration();
$connectionParams = array(
    'user'     => DB_USERNAME,
    'password' => DB_PASSWORD,
    'pdo'      => $connPdo,
    'driverClass' =>'Doctrine\DBAL\Driver\PDOIbm\Driver',
);

$connection = DriverManager::getConnection($connectionParams, $config);


$queryBuilder = $connection->createQueryBuilder();
moyarich
  • 31
  • 8