On Jun 6, 2013, at 4:04 AM, Lutischán Ferenc <lutisch...@gmail.com> wrote:

> Dear Daniel,

Please don't top post.  It makes it hard to follow the conversation.  Either 
post your response inline, like I have been doing, or post below previous 
responses.

> 
> Thanks. The slow query log works. :-)

Good to hear!

> 
> The code doesn't work. It called from a cron4j under Tomcat:

Two questions:

1.) Where do you have your data source defined?  Is this in conf/server.xml, 
conf/context.xml, conf/Catalina/localhost/<app-name>.xml or 
META-INF/context.xml?

2.) Where are your classes below located?  The $CATALINA_BASE/lib directory?  
WEB-INF/lib?


> public class SchedulerServletContextListener implements 
> ServletContextListener {
> 
>    private static final String SCHEDULER = "cron4j.scheduler";
> 
>    @Override
> 
>    public void contextInitialized(ServletContextEvent event) {
> 
>        ServletContext context = event.getServletContext();
> 
>        Scheduler scheduler = new Scheduler();
> 
>        Task slowQueryReport = new SlowQueryReport();
> 
>        try {
> 
>            if (Tools.isTest()) {
> 
>                scheduler.schedule("0 23 * * *", slowQueryReport);
> 
>            }
> 
>        } catch (UnknownHostException ex) {
> 
> Logger.getLogger(SchedulerServletContextListener.class.getName()).log(Level.SEVERE,
>  ex.toString(), ex);
> 
>        }
> 
>        scheduler.start();
> 
>        context.setAttribute(SCHEDULER, scheduler);
> 
>    }
> 
>    @Override
> 
>    public void contextDestroyed(ServletContextEvent event) {
> 
>        ServletContext context = event.getServletContext();
> 
>        Scheduler scheduler = (Scheduler) context.getAttribute(SCHEDULER);
> 
>        context.removeAttribute(SCHEDULER);
> 
>        scheduler.stop();
> 
>    }
> 
> }
> 
> public class SlowQueryReport extends Task {
> 
>    @Override
> 
>    public void execute(TaskExecutionContext executor) throws RuntimeException 
> {
> 
>        StringBuilder report = new StringBuilder();
> 
>        report.append(getStat("x"));
> 
>        report.append(getStat("x"));
> 
>        report.append(getStat("x"));
> 
>        report.append(getStat("x"));
> 
> MailSender.sendDirectMail("sen...@x.hu", "x...@x.hu", "Slow Query Report", 
> report.toString());
> 
>    }
> 
>    private String getStat(String poolName) {
> 
>        StringBuilder result = new StringBuilder();
> 
>        Map<String, SlowQueryReportJmx.QueryStats> map = 
> SlowQueryReportJmx.getPoolStats("jdbc/" + poolName);
> 
>        if (map == null) {
> 
>            result.append("No pool: ").append(poolName);
> 
>        } else {
> 
>            for (Map.Entry<String, SlowQueryReportJmx.QueryStats> entry : 
> map.entrySet()) {
> 
> result.append(entry.getKey()).append('\t').append(entry.getValue().toString());
> 
>            }
> 
>        }
> 
>        return result.toString();
> 
>    }
> 
> }

What are you trying to achieve here?  It looks like you are trying to email the 
slow query logs somewhere, is that right?

If that's the case, maybe you could do something like this…
  1.) Configure logging.properties to write the SlowQuery logs to a dedicated 
log file.
  2.) Run an OS cron job that grabs the file and emails it 

Alternatively, you could use Log4j and it's SMTP appender to send the logs via 
email.

Dan


> 
> Thanks,
>        Ferenc
> 
> On 2013.06.05. 16:54, Daniel Mikusa wrote:
>> On Jun 5, 2013, at 4:10 AM, Lutischán Ferenc <lutisch...@gmail.com> wrote:
>> 
>>> Dear Daniel,
>>> 
>>> Thanks for your reply.
>>> My Tomcat version is 7.0.35. It is under CentOS 5.8.
>>> 
>>> My datasource config is:
>>> <Resource name="jdbc/xxx" auth="Container" 
>>> driverClassName="org.postgresql.Driver" 
>>> factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="1" 
>>> maxIdle="20" maxActive="20" maxWait="5000" password="xxx" 
>>> type="javax.sql.DataSource" url="jdbc:postgresql://10.3.76.41/xxx" 
>>> username="xxx" validationQuery="select 1" 
>>> jdbcInterceptors="ConnectionState;SlowQueryReportJmx(threshold=1000)" 
>>> testOnBorrow="true"/>
>>> 
>>> There is my java code:
>>>        Map<String, SlowQueryReportJmx.QueryStats> map = 
>>> SlowQueryReportJmx.getPoolStats("java:comp/env/jdbc/" + poolName);
>> And where are you trying to run this?
>> 
>>> I tried the config with 
>>> jdbcInterceptors="ConnectionState;SlowQueryReport(threshold=1000)",
>>> but there is no messages in the Tomcat log files.
>> With both SlowQueryReport and SlowQueryReportJmx, you should see entries in 
>> your log files when a slow query occurs.  In the case of your configuration, 
>> a slow query would be one that lasts longer than one second.
>> 
>> If you have a query that runs for longer than one second, you'll see a log 
>> entry at the WARN level and it'll start with "Slow Query Report SQL=".
>> 
>> If you don't see these…
>> 
>>   1.) Make sure that your application is taking a connection from the 
>> connection pool and using that to execute the query.
>>   2.) Make sure that your query is actually running for longer than one 
>> second or for testing purposes lower the threshold so that your query runs 
>> longer than the threshold.
>>   3.) Make sure that your logging configuration is not preventing the entry 
>> from being displayed.  The default logging.properties file that is included 
>> with Tomcat should show these messages.
>> 
>> Dan
>> 
>>> Thanks,
>>>      Ferenc
>>> 
>>> 2013.06.04. 16:20 keltezéssel, Daniel Mikusa írta:
>>>> On Jun 4, 2013, at 6:26 AM, Lutischán Ferenc <lutisch...@gmail.com> wrote:
>>>> 
>>>>> Dear Users,
>>>>> 
>>>>> Please help to me:
>>>>> How to get SlowQueryReport statistics?
>>>> Usually these are logged as WARN messages.  Unless you use 
>>>> SlowQueryReportJmx and then they are logged and sent as JMX notifications.
>>>> 
>>>>> I tried:
>>>>> 
>>>>> Map<String,SlowQueryReport.QueryStats> map = 
>>>>> SlowQueryReport.getPoolStats("java:comp/env/jdbc/xxx");
>>>>> and
>>>>> Map<String,SlowQueryReport.QueryStats> map = 
>>>>> SlowQueryReport.getPoolStats("xxx");
>>>>> 
>>>>> The result in both case was a null map.
>>>> Can you elaborate on what you are trying to do here?
>>>> 
>>>> Are you using Tomcat?
>>>> 
>>>> If so…
>>>>   1.) What version are you using?
>>>>   2.) How do you have your data source configured?  Please include the 
>>>> resource tag.
>>>> 
>>>> If not…
>>>>   1.) Are you using the Tomcat's jdbc-pool directly?
>>>>   2.) How are you setting it up?
>>>>   3.) How is it configured?
>>>>   4.) What version are you using?
>>>> 
>>>> Dan
>>>> 
>>>>> Thanks,
>>>>>       Ferenc
>>>>> 
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>>>>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>>>> 
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>>>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>>> 
>>>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>> For additional commands, e-mail: users-h...@tomcat.apache.org
>> 
>> 
> 


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

Reply via email to