0

I have a simple spring standalone application where I creating my own datasource using apache basic datasource class. Data source is then passed to Named parameter jdbc template.

The first query is always taking more than 12 secs to execute - then the second query is fine. (I believe this is happening because it establishes connection for the first time). But this is hampering performance of the application as it is supposed to be running for every 5 mins but one job itself is taking more than the usual time. Can someone please tell me what can be done to improve the performance. Here is the code:

--- Datasource creation and template object creation --------

@Configuration
@PropertySource(value = { "file:///opt/dsp/share/Batchjobs/GRComponent/conf/application.properties" })
public class DataSourceConfig {

    private static Log log = LogFactory.getLog(DataSourceConfig.class);

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        long start = System.currentTimeMillis();
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
        /*DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(env.getRequiredProperty("jdbc.password"));*/
        long end = System.currentTimeMillis();
        if(log.isDebugEnabled())
        log.debug("Total time for creating datasource: " +(end - start));       
        return dataSource;
    }

    @Bean
    public NamedParameterJdbcTemplate namedJdbcTemplate(DataSource dataSource) {

        long start = System.currentTimeMillis();
        NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        long end = System.currentTimeMillis();
        log.info("Total time for NamedParameterJdbcTemplate: " +(end - start));
        return jdbcTemplate;
    }

-----First query call

SiteDetails sd = null; 
        try{
            String fetchSiteDtls = "SELECT SITE_STATUS_ID, SITE_ENABLED, ITERATION_ID, UPDATED_DATE "+ 
                "FROM DSP_SITE_DETAILS WHERE SITE_ID =:siteid";

        MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        parameterSource.addValue("siteid", siteId);
        if(log.isDebugEnabled())
        log.debug("fetchSiteDetails:query:" +fetchSiteDtls);
        long start = System.currentTimeMillis();

        sd = namedParJdbcTempl.queryForObject(fetchSiteDtls, parameterSource, (rs, rowNum)->{                           
                    SiteDetails siteDetails = new SiteDetails();
                    siteDetails.setSiteStatus(rs.getInt("SITE_STATUS_ID"));
                    siteDetails.setSiteEnabled(rs.getString("SITE_ENABLED"));
                    siteDetails.setIterationId(rs.getInt("ITERATION_ID"));
                    siteDetails.setStatusUpdateTimeStamp(rs.getTimestamp("UPDATED_DATE"));                  
                    return siteDetails;});
        sd.setSiteId(siteId);
            long end = System.currentTimeMillis();
            if(log.isDebugEnabled())
            log.debug("Total time for executing query: " +(end - start));               
            return sd;

Question updated to state: Oracle is the database and query is already ran in sql client to run less than 1 sec. Even tried different sql. Tried different ways of connecting to datasource like spring datasource, apache basic datasource, HikerCP datasource, finally removed datasource itself and tried with normal jdbc connection with driver manager. I added execution time for every step and got that the connection object is taking 12 - 20 secs to connect. 

Can someone from oracle community help with a suggestion as can there be any problem with the oracle setup or something else?
Prakruti Pathik
  • 392
  • 4
  • 17
  • Where are you calculating time taken by the query? – Vikram Singh Sep 16 '19 at 07:32
  • There are so many parameters you can configure like active connections, test them using the sample query and max active time. Read the documentation and use the params which are suitable for your case. e.g., https://stackoverflow.com/a/8553306/1423818 And I think DBCP is less used now a days so sitch to better libraries like [hikariCp](https://brettwooldridge.github.io/HikariCP/). – mallikarjun Sep 16 '19 at 07:37
  • @VikramSingh : I edited the post to add the timestamp in the code. – Prakruti Pathik Sep 16 '19 at 09:00
  • You are not calculating the time correctly – Vikram Singh Sep 16 '19 at 09:03
  • @VikramSingh: Sorry one second I edited the query. Changed the first query call that was initial for test purpose. – Prakruti Pathik Sep 16 '19 at 09:06
  • @Mallikarjun: I tried HikariCP but the query was still taking time. So I removed the datasource creation and went straight with jdbc way of connecting to database. Added the time stamp each and it turns out that even with jdbc first query execution is taking time. I added timestamps for each step and found out that getting connection is taking more than 15 secs. Can you please help what could be the problem. Can it be something to do with the way database is setup like its conf or something? – Prakruti Pathik Sep 17 '19 at 05:34
  • @PrakrutiPathik Here is the connection issue reported in [github](https://github.com/brettwooldridge/HikariCP/issues/1366) and which DB you are using? Have you try to execute the query in DB tools and does it taking too much time over there too? One more suggestion enable Hicakri logs so that it will print the tome and the process it took the get connection rather than your sysout of time. – mallikarjun Sep 17 '19 at 05:45
  • @mallikarjun: The database is Oracle. Query I already executed in the client, it took less than 1 sec and I even tried executing a different query as the first query (changed the sql to simple select systimestamp from dual) - so nothing to do with query i know. For connection issue, it was there even with spring datasource, apache datasource, Hikari tool and now java jdbc connection way. Everything takes time and it happens only with the first query no matter what is the first query. I need suggestion if problem is with the setup or any specific configuration in code for DS etc... – Prakruti Pathik Sep 17 '19 at 05:53
  • @PrakrutiPathik better to tag this for oracle and it seems everything fine from the java side. – mallikarjun Sep 17 '19 at 05:59

0 Answers0