Spring Hibernate “Closed Statement / ResultSet”

I was encountering this exception, complaining about “cannot operate on closed statement or resultset”.

I have a domainClass map to one table. The domain class use an ID class for composite identity. The domain class also use another class to resolve one property.

<hibernate-mapping>
	<class name="AbstractHibernatePredicate" table="jql_mapping" lazy="false" mutable="true" discriminator-value="not null">
		<cache usage="read-only"/>
    	<composite-id name="jqlMappingID" class="com.bfm.predicate.JqlMappingID" >
			<key-property name="purpose" column="purpose" type="&TrimmedString;" />
			<key-property name="evalOrder" column="eval_order" />
    	</composite-id>
    	
		<property name="xxxx" column="xxxx" type="&TrimmedString;" />
		<subclass name="ConcreteHibernatePredicate" discriminator-value="not null">
            <property name="expression"  column="jql" type="SomeExpressionType"/>
        </subclass>
	</class>
</hibernate-mapping>

SomeExpressionType is org.hibernate.usertype.CompositeUserType. It overrides nullsafeget

    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
        String expr = null;
        try {
        	String prog = resolveProgram(rs);
            if (rs.isFirst() || macroFactory.get() == null || !macroFactory.get().containsKey(prog)) {
                @SuppressWarnings("unchecked")
                List<xx> xx= session.getNamedQuery("byProgram").list();
                 ....
            }
            return getParsedExpression(rs.getString(names[0]),prog);
        } catch (Exception e) {
            log.warn("Exception while parsing BQLExpression. Failed expression: " + expr + ". Error " + e.getMessage());
            return new BQLExpression("1=2");
        }
    }

within above method, it use same session to invoke another query and execute. This results the existing resutlSet detached, because this configuration:

<prop key="hibernate.connection.release_mode">after_transaction</prop>

or

<prop key="hibernate.connection.release_mode">after_statement</prop>

the only way to work is

<!--prop key="hibernate.connection.release_mode">after_statement</prop-->

or

<prop key="hibernate.connection.release_mode">on_close</prop>

http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/session-configuration.html

Advertisements