Should I use StringBuffer or StringBuilder for SQL statement assembly?

Asked 2 years ago, Updated 2 years ago, 496 views

There are Tomcat, which is accessed by multiple people at the same time, and web applications that run on IIS.
SQL statements are dynamically assembled within the application, taking into account on-screen input at runtime.
(Not just string concatenation.)
[Supplement]
JdbcTemplate, using NamedParameterJdbcTemplate

The thread-safe StringBuffer class is used for this SQL statement assembly process. When I was asked to rewrite to StringBuilder, I was looking for evidence.
I found the following answers to other people's questions on this site:

Furthermore, StringBuffer is a fairly old class and is no longer used unless there are special conditions.Typically, use the StringBuilder class.

The StringBuffer#append method is synchronized, but it's not just an old or new perspective.
What is the basis for using StringBuilder when using it for web applications?
Also, if you don't care about the length of processing time,
What are the "special conditions" that apply here?
Please let me know if you know the expected situation.

java spring

2022-11-21 16:38

2 Answers

In this case, I think it would be better to refer to the primary source, the official reference.

StringBuffer:

Variable thread-safe string.
(omitted)
Beginning with JDK5, this class is supplemented with an equivalent class designed to be used by a single thread StringBuilder. The StringBuilder class supports all the same processing as this class but is fast and does not perform synchronization.

StringBuilder:

Variable sequence of characters. This class provides an API that is compatible with StringBuffer, but synchronization is not guaranteed. This class is designed to be used as a simple alternative to StringBuffer when the string buffer is used by a single thread (common case). Because this class runs faster in most implementations, it is recommended that you prefer it to StringBuffer when possible.
(omitted)
The StringBuilder instance is not secure for multiple threads. We recommend that you use StringBuffer if you need such synchronization.

So,

What are the reasons for using StringBuilder?

That's because the formula says so.
The reason why the formula says that is (it seems that the questioner already understands it)but) because the StringBuffer has lower performance than the StringBuilder to ensure thread safety (not required in common cases).

What are the "special conditions" that apply here?

A non-common case in a quote, where multiple threads are expected to access a single string buffer.

As you can see from the official reference, you are asked why you should use StringBuffer instead of StringBuilder.

I am using the StringBuffer class which is thread safe.

Is that thread safety necessary?is the issue.


2022-11-21 18:17

I don't recommend creating SQL statements by linking strings (=hard to read).
You should use a prepared statement.


2022-11-21 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.