1

I am using a H2 database for reading data. I was configuring the connection with spring:

    <bean id="dataSourceH2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.h2.driver}" />
        <property name="url" value="${jdbc.h2.url}" />
        <property name="username" value="${jdbc.h2.user}" />
        <property name="password" value="${jdbc.h2.password}" />
    </bean>

And using it as:

result = this.namedParameterJdbcTemplateH2.query(
            this.queryName, mapParameters, new H2DataExtractor())

Very convenient.

That was until I realized that the result set coming back from my queries was non scrollable:

org.h2.jdbc.JdbcSQLException:
The result set is not scrollable and can not be reset.
You may need to use conn.createStatementconn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ..).

So I am creating the connection by hand now:

    Connection conn = null;
    try {
        Class.forName("org.h2.Driver");
        conn = DriverManager.getConnection("jdbc:h2:path/to/my/database/file", "sa", "sa");
        final Statement st = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY
             );
            final ResultSet rs = st.executeQuery("SELECT * FROM SCHEMA1.TABLE1 WHERE id = "
                + id + " AND name = " + name + " ORDER BY color, size" );
             resultado = (new CentrosDistribucionDVLExtractor()).extractData(rs);
    } catch ....

Not that convenient.

How can I create the same connection with spring, but setting the result set to be ResultSet.TYPE_SCROLL_INSENSITIVE?

manuelvigarcia
  • 1,696
  • 1
  • 22
  • 32

0 Answers0