Skip to content

Commit 259a9bb

Browse files
dev3waybeikov
authored andcommitted
HHH-19005 Memory leak modify String to StringBuilder
1 parent ff5fa10 commit 259a9bb

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,44 @@ public String perform() {
7070
while ( tokens.hasMoreTokens() ) {
7171
token = tokens.nextToken();
7272

73-
if ( "-".equals(token) && result.toString().endsWith("-") ) {
73+
if ( "-".equals( token ) && result.toString().endsWith( "-" ) ) {
7474
do {
7575
result.append( token );
7676
token = tokens.nextToken();
7777
}
7878
while ( !"\n".equals( token ) && tokens.hasMoreTokens() );
7979
}
8080

81-
lcToken = token.toLowerCase(Locale.ROOT);
82-
switch (lcToken) {
81+
lcToken = token.toLowerCase( Locale.ROOT );
82+
switch ( lcToken ) {
8383

8484
case "'":
8585
case "`":
8686
case "\"":
8787
String t;
88+
StringBuilder sb = new StringBuilder();
89+
sb.append( this.token );
8890
do {
8991
t = tokens.nextToken();
90-
token += t;
92+
sb.append( t );
9193
}
9294
while ( !lcToken.equals( t ) && tokens.hasMoreTokens() );
95+
this.token = sb.toString();
9396
lcToken = token;
9497
misc();
9598
break;
9699
// SQL Server uses "[" and "]" to escape reserved words
97100
// see SQLServerDialect.openQuote and SQLServerDialect.closeQuote
98101
case "[":
99102
String tt;
103+
StringBuilder sb2 = new StringBuilder();
104+
sb2.append( this.token );
100105
do {
101106
tt = tokens.nextToken();
102-
token += tt;
107+
sb2.append( tt );
103108
}
104109
while ( !"]".equals( tt ) && tokens.hasMoreTokens() );
110+
this.token = sb2.toString();
105111
lcToken = token;
106112
misc();
107113
break;
@@ -238,7 +244,7 @@ private void from() {
238244
}
239245

240246
private void comma() {
241-
if ( afterByOrSetOrFromOrSelect && inFunction==0 ) {
247+
if ( afterByOrSetOrFromOrSelect && inFunction == 0 ) {
242248
commaAfterByOrFromOrSelect();
243249
}
244250
// else if ( afterOn && inFunction==0 ) {
@@ -313,7 +319,7 @@ private void beginCase() {
313319

314320
private void misc() {
315321
out();
316-
if ( afterInsert && inFunction==0 ) {
322+
if ( afterInsert && inFunction == 0 ) {
317323
newline();
318324
afterInsert = false;
319325
}
@@ -329,7 +335,7 @@ private void white() {
329335
}
330336

331337
private void updateOrInsertOrDelete() {
332-
if ( indent>1 ) {
338+
if ( indent > 1 ) {
333339
//probably just the insert SQL function
334340
out();
335341
}
@@ -367,7 +373,7 @@ private void select() {
367373

368374
private void out() {
369375
if ( result.charAt( result.length() - 1 ) == ',' ) {
370-
result.append(" ");
376+
result.append( " " );
371377
}
372378
result.append( token );
373379
}
@@ -507,7 +513,7 @@ private static boolean isWhitespace(String token) {
507513

508514
private void newline() {
509515
result.append( System.lineSeparator() )
510-
.append( INDENT_STRING.repeat(indent) );
516+
.append( INDENT_STRING.repeat( indent ) );
511517
beginLine = true;
512518
}
513519
}

hibernate-core/src/test/java/org/hibernate/orm/test/jdbc/util/BasicFormatterTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* @author Steve Ebersole
2222
*/
2323
public class BasicFormatterTest extends BaseUnitTestCase {
24-
2524
@Test
2625
public void testNoLoss() {
2726
assertNoLoss( "insert into Address (city, state, zip, \"from\") values (?, ?, ?, 'insert value')" );
@@ -47,6 +46,30 @@ public void testNoLoss() {
4746
"(select p.pid from Address where city = 'Boston') union (select p.pid from Address where city = 'Taipei')"
4847
);
4948
assertNoLoss( "select group0.[order] as order0 from [Group] group0 where group0.[order]=?1" );
49+
assertNoLoss( """
50+
INSERT INTO TEST_TABLE (JSON) VALUES
51+
('{
52+
"apiVersion": "2.0",
53+
"data": {
54+
"updated": "2010-01-07T19:58:42.949Z",
55+
"totalItems": 800,
56+
"startIndex": 1,
57+
"itemsPerPage": 1,
58+
"items": [
59+
{
60+
"id": "hYB0mn5zh2c",
61+
"uploaded": "2007-06-05T22:07:03.000Z",
62+
"updated": "2010-01-07T13:26:50.000Z",
63+
"uploader": "GoogleDeveloperDay",
64+
"category": "News",
65+
"title": "Google Developers Day US - Maps API Introduction",
66+
"description": "Google Maps API Introduction ..."
67+
}
68+
]
69+
}
70+
}')
71+
"""
72+
);
5073
}
5174

5275
@Test

0 commit comments

Comments
 (0)