views:

138

answers:

0

I have a question regarding using Prepared Statements with JBoss and MySQL.

DataSource is configured on JBoss side. Here is the config file:

x jdbc:mysql://x com.mysql.jdbc.Driver x x 10 20 5000 5 org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker 50 true some arbitrary sql --> some arbitrary sql -->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
   <type-mapping>mySQL</type-mapping>
</metadata>

With this config file Prepared Statements do not work. I found that I have to set useServerPrepStmts=true to enable server side prepared statements. Is there anything else I have to do?

Below is a sample code to ilustrate the usage of Prepared Statements in Java code:

public void foo() {

Connection connenction = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
PreparedStatement statement = null;

try {
 Connection connection = dataSource.getConnection(); //ctx lookup
 statement = connection.prepareStatement(sqlQuery);
 statement.setString(1, sampleParam);
 statement.setString(2, sampleParam);
 resultSet = preparedStatement.executeQuery();
} catch (Exception e) {
 //Exception handling logic
} finally {
 statement.close();
 resultSet.close();
 connection.close();
}

}

Is there anything in the code above I have to add/remove to use Prepared STatements in a proper way?

Would it be possible to force JBoss to prepcompile a query for a connection before returning connection to Java application in order to improve the performance? - something like precompiled named queries in Hibernate.