16
16
17
17
import javax .sql .DataSource ;
18
18
19
+ import com .alibaba .druid .pool .DruidDataSource ;
19
20
import org .apache .commons .lang .StringUtils ;
20
21
import org .joda .time .DateTime ;
21
22
import org .joda .time .DateTimeZone ;
@@ -38,7 +39,15 @@ public class Util {
38
39
public static Object sqlRS (DataSource ds , String sql , Function <ResultSet , Object > fun ) {
39
40
try (Connection conn = ds .getConnection ();
40
41
Statement stmt = conn .createStatement (ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY )) {
41
- stmt .setFetchSize (Integer .MIN_VALUE );
42
+
43
+ DruidDataSource druidDataSource = (DruidDataSource ) ds ;
44
+ if ("postgresql" .equals (druidDataSource .getDbType ())) {
45
+ conn .setAutoCommit (false );
46
+ stmt .setFetchSize (1000 );
47
+ } else {
48
+ stmt .setFetchSize (Integer .MIN_VALUE );
49
+ }
50
+
42
51
try (ResultSet rs = stmt .executeQuery (sql )) {
43
52
return fun .apply (rs );
44
53
}
@@ -52,7 +61,13 @@ public static Object sqlRS(DataSource ds, String sql, List<Object> values, Funct
52
61
try (Connection conn = ds .getConnection ()) {
53
62
try (PreparedStatement pstmt = conn
54
63
.prepareStatement (sql , ResultSet .TYPE_FORWARD_ONLY , ResultSet .CONCUR_READ_ONLY )) {
55
- pstmt .setFetchSize (Integer .MIN_VALUE );
64
+ DruidDataSource druidDataSource = (DruidDataSource ) ds ;
65
+ if ("postgresql" .equals (druidDataSource .getDbType ())) {
66
+ conn .setAutoCommit (false );
67
+ pstmt .setFetchSize (1000 );
68
+ } else {
69
+ pstmt .setFetchSize (Integer .MIN_VALUE );
70
+ }
56
71
if (values != null ) {
57
72
for (int i = 0 ; i < values .size (); i ++) {
58
73
pstmt .setObject (i + 1 , values .get (i ));
0 commit comments