-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting the session timezone to UTC on every connection
- Loading branch information
1 parent
701d7e6
commit d3c0ad7
Showing
2 changed files
with
85 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
cwms-data-api/src/main/java/cwms/cda/datasource/SessionTimeZonePreparer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cwms.cda.datasource; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import javax.annotation.Nullable; | ||
|
||
public class SessionTimeZonePreparer implements ConnectionPreparer { | ||
|
||
public SessionTimeZonePreparer() { | ||
|
||
} | ||
|
||
private static @Nullable String getSessionTimeZone(Connection connection) throws SQLException { | ||
String dbTimeZone = null; | ||
String sql = "SELECT sessiontimezone FROM dual"; | ||
try (Statement statement = connection.createStatement(); | ||
ResultSet resultSet = statement.executeQuery(sql)) { | ||
if (resultSet.next()) { | ||
dbTimeZone = resultSet.getString(1); | ||
} | ||
} | ||
return dbTimeZone; | ||
} | ||
|
||
private static void setSessionTimeZoneUtc(Connection connection) throws SQLException { | ||
String sql = "ALTER SESSION SET TIME_ZONE = 'UTC'"; | ||
try (CallableStatement statement = connection.prepareCall(sql)) { | ||
statement.execute(); | ||
} | ||
} | ||
|
||
@Override | ||
public Connection prepare(Connection conn) throws SQLException { | ||
setSessionTimeZoneUtc(conn); | ||
return conn; | ||
} | ||
|
||
} |