@@ -128,6 +128,7 @@ public class RubyJdbcConnection extends RubyObject {
128
128
private boolean lazy = false ; // final once set on initialize
129
129
private boolean jndi ; // final once set on initialize
130
130
private boolean configureConnection = true ; // final once initialized
131
+ private int fetchSize = 0 ; // 0 = JDBC default
131
132
132
133
protected RubyJdbcConnection (Ruby runtime , RubyClass metaClass ) {
133
134
super (runtime , metaClass );
@@ -571,6 +572,11 @@ protected void doInitialize(final ThreadContext context, final IRubyObject confi
571
572
else {
572
573
this .configureConnection = value != context .runtime .getFalse ();
573
574
}
575
+
576
+ IRubyObject jdbcFetchSize = getConfigValue (context , "jdbc_fetch_size" );
577
+ if (jdbcFetchSize != context .nil ) {
578
+ this .fetchSize = RubyNumeric .fix2int (jdbcFetchSize );
579
+ }
574
580
}
575
581
576
582
@ JRubyMethod (name = "adapter" )
@@ -818,6 +824,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
818
824
// is called, so we have to process the result sets as we get them
819
825
// this shouldn't be an issue in most cases since we're only getting 1 result set anyways
820
826
result = mapExecuteResult (context , connection , resultSet );
827
+ resultSet .close ();
821
828
} else {
822
829
result = context .runtime .newFixnum (updateCount );
823
830
}
@@ -851,6 +858,7 @@ protected Statement createStatement(final ThreadContext context, final Connectio
851
858
else {
852
859
statement .setEscapeProcessing (escapeProcessing .isTrue ());
853
860
}
861
+ if (fetchSize != 0 ) statement .setFetchSize (fetchSize );
854
862
return statement ;
855
863
}
856
864
@@ -1045,6 +1053,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
1045
1053
else {
1046
1054
final PreparedStatement prepStatement ;
1047
1055
statement = prepStatement = connection .prepareStatement (query );
1056
+ if (fetchSize != 0 ) statement .setFetchSize (fetchSize );
1048
1057
statement .setMaxRows (maxRows ); // zero means there is no limit
1049
1058
setStatementParameters (context , connection , prepStatement , binds );
1050
1059
hasResult = prepStatement .execute ();
@@ -1123,7 +1132,9 @@ public IRubyObject prepare_statement(final ThreadContext context, final IRubyObj
1123
1132
return withConnection (context , new Callable <IRubyObject >() {
1124
1133
public IRubyObject call (Connection connection ) throws SQLException {
1125
1134
final String query = sql .convertToString ().getUnicodeValue ();
1126
- return JavaUtil .convertJavaToRuby (context .runtime , connection .prepareStatement (query ));
1135
+ PreparedStatement statement = connection .prepareStatement (query );
1136
+ if (fetchSize != 0 ) statement .setFetchSize (fetchSize );
1137
+ return JavaUtil .convertJavaToRuby (context .runtime , statement );
1127
1138
}
1128
1139
});
1129
1140
}
@@ -1158,19 +1169,15 @@ public IRubyObject call(final Connection connection) throws SQLException {
1158
1169
statement = (PreparedStatement ) JavaEmbedUtils .rubyToJava (cachedStatement );
1159
1170
} else {
1160
1171
statement = connection .prepareStatement (query );
1172
+ if (fetchSize != 0 ) statement .setFetchSize (fetchSize );
1161
1173
}
1162
1174
1163
1175
setStatementParameters (context , connection , statement , (RubyArray ) binds );
1164
1176
1165
1177
if (statement .execute ()) {
1166
1178
ResultSet resultSet = statement .getResultSet ();
1167
1179
IRubyObject results = mapQueryResult (context , connection , resultSet );
1168
-
1169
- if (cached ) {
1170
- // Make sure we free the result set if we are caching the statement
1171
- // It gets closed automatically when the statement is closed if we aren't caching
1172
- resultSet .close ();
1173
- }
1180
+ resultSet .close ();
1174
1181
1175
1182
return results ;
1176
1183
} else {
@@ -2501,6 +2508,8 @@ protected IRubyObject arrayToRuby(final ThreadContext context,
2501
2508
while ( arrayResult .next () ) {
2502
2509
array .append ( jdbcToRuby (context , runtime , 2 , baseType , arrayResult ) );
2503
2510
}
2511
+ arrayResult .close ();
2512
+
2504
2513
return array ;
2505
2514
}
2506
2515
finally { if ( value != null ) value .free (); }
0 commit comments