0

I am developing a batch using Spring. To perform the query I use JdbcTemplate.

In my specific case JdbcTemplate is provided to the DAO class extending the Spring JdbcDaoSupport class, so I have the following DAO:

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PucManagerColl extends JdbcDaoSupport {

private static Logger log = Logger.getLogger(PucManagerProd.class);

    public PucManagerColl() {
        System.out.println("Costruzione PucManager");
    }

    @Autowired
    public PucManagerColl(@Qualifier("dataSourcePUCColl") DataSource dataSource) {
        setDataSource(dataSource);

    }

    ...............................................................
    ...............................................................

        public void insertTirConsolidatoPolizza(TassoRendimentoInterno tri) {

        log.debug("PucManagerColl.insertTirConsolidatoPolizza()");

        String sql = "INSERT INTO TirConsolidatoPolizza "
                   + "("
                   + "Polizzaid, "
                   + "PercentualeRendimentoDaInizioGestione, "
                   + "PercentualeRendimentoDaInizioAnno, "
                   + "PercentualeRendimentoDaInizioTrimestre, "
                   + "ControvaloreFinaleBF, "
                   + "DataRiferimentoNav"
                   + ") "
                   + "VALUES (?, ?, ?, ?, ?, ?, ?)";


        this.getJdbcTemplate().update(sql,  
                  new Object[] { tri.getPolizzaID(),
                                 tri.getPercRendimentoInizioSottoscrizione(),
                                 tri.getPercRendimentoInizioAnno(),
                                 tri.getPercRendimentoInizioTrimestre(),
                                 tri.getControvaloreQuote(),
                                 tri.getDataRiferimentoNavPUC()});


        log.debug("TERMINATO: PucManagerColl.insertTirConsolidatoPolizza()");
    }
}

As you can see this class extends Spring class JdbcDaoSupport and the datasource is injected into the constructor.

The problem is that the insertTirConsolidatoPolizza() method can't insert the record into the TirConsolidatoPolizza table. I obtain no error and no excption. Why? Do you notice some problem in my query?

So I want debug the query genrated by JdbcTemplate and try to perform it manually into the database prompt but I can't see it.

Reading this post: Seeing the underlying SQL in the Spring JdbcTemplate?

and the official doumentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

it seems to me that I can configure JdbcTemplate to log the query in debug mode or something like it.

In the other post show this XML snippet to do it:

<category name="org.springframework.jdbc.core.JdbcTemplate">
    <priority value="debug" />
</category>

but I don't know exactly where I hae to put it.

This is my databaseConfiguration.xml that contains the informations for the DB connection (I import this file into the main configuration file applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <tx:annotation-driven transaction-manager="tjtJTransactionManager" />

    <!-- DB CONNECTIONS: -->
    <bean id="tjtJTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        scope="singleton">
        <property name="dataSource" ref="dataSourcePUC" />
    </bean>

    <!-- PROFILO DI PRODUZIONE -->
    <beans profile="PROD">

        <bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="url" value="jdbc:jtds:sqlserver://XXX.myCompany.YYYY.it:1433/DB_1" />
            <property name="username" value="username" />
            <property name="password" value="pswd" />
        </bean>

    </beans>

    <!-- PROFILO DI COLLAUDO -->
    <beans profile="COLL">

        <bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="url" value="jdbc:jtds:sqlserver://XXXX.MYCOMPANY.YYYY.it:1433/DB_2" />
            <property name="username" value="username" />
            <property name="password" value="pswd" />

        </bean>

    </beans>

</beans>

How can I configure the query logging to see the performed query in my stacktrace?

Community
  • 1
  • 1
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

2

How can I configure Spring to show the query performed by JdbcTemplate?

It depends on logging framework you use. For example, for log4j you can place following lines in log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} [%t] %m%n" />
    </layout>
  </appender>

  <root>
    <priority value ="warn" />
    <appender-ref ref="console"/>
  </root>

  <category name="org.springframework.jdbc.core.JdbcTemplate">
    <priority value="debug" />
  </category>
</log4j:configuration> 

You have to make sure log4j.xml is in the project class path. See https://logging.apache.org/log4j/2.x/manual/configuration.html for detailed configuration instructions.

PS: By the way, correct query should look like

getJdbcTemplate().update(sql,  
              tri.getPolizzaID(),
              tri.getPercRendimentoInizioSottoscrizione(),
              tri.getPercRendimentoInizioAnno(),
              tri.getPercRendimentoInizioTrimestre(),
              tri.getControvaloreQuote(),
              tri.getDataRiferimentoNavPUC());
Andrew Kolpakov
  • 429
  • 3
  • 8