I'm logging with log4j2

log4j2.xml :
<Configuration>
    <Appenders>
        <JDBC name="MySQLDatabase2" tableName="log4j2">
            <ConnectionFactory class="my.ConnectionFactory"
method="getConnection" />
            ...
        <AsyncRoot level="trace">
            <AppenderRef ref="MySQLDatabase2" />

ConnectionFactory class :
    public static Connection getConnection() throws SQLException {
            dataSource = new org.apache.commons.dbcp2.BasicDataSource();
            dataSource.setUp ( url, user, pass, driver )

            dataSource.setDefaultAutoCommit(false);
            dataSource.setAutoCommitOnReturn(false);
            dataSource.setRollbackOnReturn(false);
            dataSource.setValidationQuery("SELECT 1");

//        Connection connection = dataSource.getConnection();
//        connection.setAutoCommit(true); // no change, first is SET
autocommit=(0|1) and log4j2 next is SET autocommit=0
//        return connection;
        return dataSource.getConnection();

In code call :
org.apache.logging.log4j.Logger.trace("hello")

Finally calling logger,
use JDBC Appender
and get a connection from the BasicDataSource pool.
And continu outside my code with the unwanted transactional statement.
For writing log in my database.

-----Message d'origine-----
De : Gary Gregory <garydgreg...@gmail.com>
Envoyé : jeudi 2 juin 2022 11:23
À : Commons Users List <user@commons.apache.org>
Objet : Re: DBCP Database Connection Pools without transactionnal NO
autocommit

Something is not clear to me: are you in control of calling JDBC or are you
using Log4j's JDBC Appender or something else? You description is not
detailed enough for me to understand who does what.

Gary

On Wed, Jun 1, 2022, 19:49 Sébastien Jachym <
sebastien.jac...@agri-commerce.fr> wrote:

> Hello,
> Thank you Gary for you reply, but it doesn’t work with configuring
> BasicDataSource with :
>    dataSource.setDefaultAutoCommit(false);
>    dataSource.setAutoCommitOnReturn(false);
>    dataSource.setRollbackOnReturn(false);
>
> There are changes, but always transactional with "SET autocommit" :
> SELECT @@session.transaction_isolation SELECT 1 -- ValidationQuery
> dans ConnectionFactory SET autocommit=0 SET SQL_SELECT_LIMIT=1 select
>
> date,logger,level,message,exception,pid,idUtilisateur,version,ip,marke
> rSimpleName,sqlLastInsertId,sqlTimeMillis
> from log4j2 where 1=0
> SET SQL_SELECT_LIMIT=DEFAULT
> insert into log4j2
>
> (date,logger,level,message,exception,pid,idUtilisateur,version,ip,mark
> erSimpleName,sqlLastInsertId,sqlTimeMillis)
> values (... the values ...)
> commit
>
> Also before returning the connection to my log4j2 Async Appenders
> whith ConnectionFactory, I force : connection.setAutoCommit(false);
> But result is same with transactional statements :
> SET autocommit=0 -- [ OR "SET autocommit=0" WITH
> connection.setAutoCommit(true); ] SET autocommit=0 SET
> SQL_SELECT_LIMIT=1 select
>
> date,logger,level,message,exception,pid,idUtilisateur,version,ip,marke
> rSimpleName,sqlLastInsertId,sqlTimeMillis
> from log4j2 where 1=0
> SET SQL_SELECT_LIMIT=DEFAULT
> insert into log4j2
>
> (date,logger,level,message,exception,pid,idUtilisateur,version,ip,mark
> erSimpleName,sqlLastInsertId,sqlTimeMillis)
> values ...
> commit
>
> Cheers.
>
>
> -----Message d'origine-----
> De : Gary Gregory <garydgreg...@gmail.com> Envoyé : mercredi 1 juin
> 2022 14:57 À : Commons Users List <user@commons.apache.org> Objet :
> Re: DBCP Database Connection Pools without transactionnal NO
> autocommit
>
> Hello,
>
> Try calling Connection#setAutoCommit(false) once you get a Connection.
> You'll likely want to configure your data source with
> org.apache.commons.dbcp2.BasicDataSource.setAutoCommitOnReturn(false)
> to avoid resetting the auto-commit flag.
>
> Gary
>
> On Tue, May 31, 2022 at 7:46 PM Sébastien Jachym
> <sebastien.jac...@agri-commerce.fr> wrote:
> >
> > Hy, is a way to fully disabled transactional (autocommit) statement
> > in DBCP ?
> >
> >
> >
> > Using Database Connection Pools with
> > org.apache.commons.dbcp2.BasicDataSource give me statements :
> >
> >
> >
> > SET autocommit=1
> >
> > SET autocommit=0
> >
> > SET SQL_SELECT_LIMIT=1
> >
> > select
> > date,logger,level,message,exception,pid,idUtilisateur,version,ip,mar
> > ke rSimpleName,sqlLastInsertId,sqlTimeMillis
> > from log4j2 where 1=0
> >
> > SET SQL_SELECT_LIMIT=DEFAULT
> >
> > insert into log4j2
> > (date,logger,level,message,exception,pid,idUtilisateur,version,ip,ma
> > rk
> > erSimpleName,sqlLastInsertId,sqlTimeMillis)
> > values
> > ('2022-06-01','timertask','TRACE','WaitInitService','','45964','123','
> > null','','','','0')
> >
> > commit
> >
> > rollback
> >
> > SET autocommit=1
> >
> >
> >
> > But it's for MySQL MyISAM table, and I didn't need transactions, I
> > whish only :
> >
> >
> >
> > select
> > date,logger,level,message,exception,pid,idUtilisateur,version,ip,mar
> > ke rSimpleName,sqlLastInsertId,sqlTimeMillis
> > from log4j2 where 1=0
> >
> > SET SQL_SELECT_LIMIT=DEFAULT
> >
> > insert into log4j2
> > (date,logger,level,message,exception,pid,idUtilisateur,version,ip,ma
> > rk
> > erSimpleName,sqlLastInsertId,sqlTimeMillis)
> > values
> > ('2022-06-01','timertask','TRACE','WaitInitService','','45964','123','
> > null','','','','0')
> >
> >
> >
> > Thanks for you replys.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
> For additional commands, e-mail: user-h...@commons.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
> For additional commands, e-mail: user-h...@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org

Reply via email to