Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit db491a6

Browse files
committed
SimpleDateFormat performance improvement, thread-safe.
Barry Lind
1 parent 3938150 commit db491a6

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ public PreparedStatement(Connection connection, String sql) throws SQLException
6565
this.sql = sql;
6666
this.connection = connection;
6767

68-
// might just as well create it here, so we don't take the hit later
69-
70-
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
71-
tl_df.set(df);
72-
73-
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
74-
tl_tsdf.set(df);
75-
7668
for (i = 0; i < sql.length(); ++i)
7769
{
7870
int c = sql.charAt(i);
@@ -95,17 +87,6 @@ public PreparedStatement(Connection connection, String sql) throws SQLException
9587
templateStrings[i] = (String)v.elementAt(i);
9688
}
9789

98-
/**
99-
* New in 7.1 - overides Statement.close() to dispose of a few local objects
100-
*/
101-
public void close() throws SQLException
102-
{
103-
// free the ThreadLocal caches
104-
tl_df.set(null);
105-
tl_tsdf.set(null);
106-
super.close();
107-
}
108-
10990
/**
11091
* A Prepared SQL query is executed and its ResultSet is returned
11192
*
@@ -343,6 +324,10 @@ public void setBytes(int parameterIndex, byte x[]) throws SQLException
343324
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
344325
{
345326
SimpleDateFormat df = (SimpleDateFormat) tl_df.get();
327+
if(df==null) {
328+
df = new SimpleDateFormat("''yyyy-MM-dd''");
329+
tl_df.set(df);
330+
}
346331

347332
set(parameterIndex, df.format(x));
348333

@@ -382,6 +367,10 @@ public void setTime(int parameterIndex, Time x) throws SQLException
382367
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
383368
{
384369
SimpleDateFormat df = (SimpleDateFormat) tl_tsdf.get();
370+
if(df==null) {
371+
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
372+
tl_tsdf.set(df);
373+
}
385374
df.setTimeZone(TimeZone.getTimeZone("GMT"));
386375

387376
// Use the shared StringBuffer

0 commit comments

Comments
 (0)