jeudi 25 mai 2017

Factory pattern with parameters

I am using the JDBC connector for my log4j output and want to post my log entries to a Postgres database. This looks as follows:

<Jdbc name="Jdbc" tableName="log">
    <ConnectionFactory class="database.ConnectionFactory" method="getConnection"/>
    <Column name="event_date" isEventTimestamp="true" />
    <Column name="level" pattern="%level" isUnicode="false" />
    <Column name="logger" pattern="%logger" isUnicode="false" />
    <Column name="message" pattern="%message" isUnicode="false" />
     <Column name="exception" pattern="%ex{full}" isUnicode="false" />
</Jdbc>

As far as I understand this requires a static class with the method getConnection and I implemented this using the factory pattern:

public class ConnectionFactory {
    private static interface Singleton {
        final ConnectionFactory INSTANCE = new ConnectionFactory();
    }

    private ComboPooledDataSource comboPooledDataSource;

    private ConnectionFactory() {
        comboPooledDataSource = new ComboPooledDataSource();
        try {
            // Load the jdbc driver.
            comboPooledDataSource.setDriverClass("org.postgresql.Driver");
        } catch (PropertyVetoException exception) {

        }

        // Need to create datasource here, requires parameters.
    }

    public static Connection getConnection() throws SQLException {
        return Singleton.INSTANCE.comboPooledDataSource.getConnection();
    }
}

My problem is, that I want to create the connection to the database via parameters (host, port, database, etc.) and do not want to hard-code it. Also having a static class which holds the configuration would not be preferred because I would like to be able to unit test it easily.

What is a good solution to achieve this? Am I maybe overlooking something or is this a bad practice?

Aucun commentaire:

Enregistrer un commentaire