forked from ebean-orm/ebean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DB2: Experimental: Add QueryPlanLogger for DB2
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ebean-core/src/main/java/io/ebeaninternal/server/query/QueryPlanLoggerDb2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
package io.ebeaninternal.server.query; | ||
|
||
import io.ebeaninternal.api.CoreLog; | ||
import io.ebeaninternal.api.SpiDbQueryPlan; | ||
import io.ebeaninternal.api.SpiQueryPlan; | ||
import io.ebeaninternal.server.type.bindcapture.BindCapture; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Random; | ||
|
||
/** | ||
* A QueryPlanLogger for DB2. | ||
* | ||
* To use query plan capturing, you have to install the explain tables with | ||
* <code>SYSPROC.SYSINSTALLOBJECTS( 'EXPLAIN', 'C' , '', CURRENT SCHEMA )</code>. | ||
* To do this in a repeatable script, you may use this statement: | ||
* | ||
* <pre> | ||
* BEGIN | ||
* IF NOT EXISTS (SELECT * FROM SYSCAT.TABLES WHERE TABSCHEMA = CURRENT SCHEMA AND TABNAME = 'EXPLAIN_STREAM') THEN | ||
* call SYSPROC.SYSINSTALLOBJECTS( 'EXPLAIN', 'C' , '', CURRENT SCHEMA ); | ||
* END IF; | ||
* END | ||
* </pre> | ||
* | ||
* @author Roland Praml, FOCONIS AG | ||
*/ | ||
public final class QueryPlanLoggerDb2 extends QueryPlanLogger { | ||
|
||
private Random rnd = new Random(); | ||
@Override | ||
public SpiDbQueryPlan collectPlan(Connection conn, SpiQueryPlan plan, BindCapture bind) { | ||
try (Statement stmt = conn.createStatement()) { | ||
int queryNo = rnd.nextInt(Integer.MAX_VALUE); | ||
try (PreparedStatement explainStmt = conn | ||
.prepareStatement("EXPLAIN PLAN SET QUERYNO=" + queryNo + " FOR " + plan.getSql())) { | ||
bind.prepare(explainStmt, conn); | ||
explainStmt.execute(); | ||
} | ||
|
||
try (ResultSet rset = stmt.executeQuery("select * from EXPLAIN_STATEMENT where QUERYNO=" + queryNo)) { | ||
return readQueryPlan(plan, bind, rset); | ||
} | ||
} catch (SQLException e) { | ||
CoreLog.log.warn("Could not log query plan", e); | ||
return null; | ||
} | ||
} | ||
|
||
} |