How to change time zone in xml audit file (Oracle 11g)? - java

i'm working on a project in java to display everything what happend in my Oracle 11g Database. For this i started using the Audit Trail in XML format because i want to use it in my network.
I created a user for example to try this out. But my Problem is the wrong timestamp in the xml files and i would need it to check the time of connections etc.
<?xml version="1.0" encoding="UTF-8"?>
<Audit xmlns="http://xmlns.oracle.com/oracleas/schema/dbserver_audittrail- 11_2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/oracleas/schema/dbserver_audittrail-11_2.xsd">
<Version>11.2</Version>
<AuditRecord><Audit_Type>1</Audit_Type><Session_Id>1260546</Session_Id><StatementId>1</StatementId><EntryId>1</EntryId><Extended_Timestamp>2016-10-01T15:59:47.473000Z</Extended_Timestamp><DB_User>CC</DB_User><OS_User>Admin-PC\Admin</OS_User><Userhost>WORKGROUP\ADMIN-PC</Userhost><OS_Process>1780:2512</OS_Process><Terminal>ADMIN-PC</Terminal><Instance_Number>0</Instance_Number><Action>100</Action><TransactionId>0000000000000000</TransactionId><Returncode>0</Returncode><Comment_Text>Authenticated by: DATABASE</Comment_Text><Priv_Used>5</Priv_Used><DBID>1434098587</DBID>
</AuditRecord>
<AuditRecord><Audit_Type>1</Audit_Type><Session_Id>1260546</Session_Id><EntryId>2</EntryId><Extended_Timestamp>2016-10-01T15:59:50.798000Z</Extended_Timestamp><DB_User>CC</DB_User><OS_User>Admin-PC\Admin</OS_User><Userhost>WORKGROUP\ADMIN-PC</Userhost><Terminal>ADMIN-PC</Terminal><Instance_Number>0</Instance_Number><Action>101</Action><Returncode>0</Returncode><DBID>1434098587</DBID>
</AuditRecord>
</Audit>
The Timestamp here is set at 15:59 but in my timezone it should be 17:59 (UTC+01:00). Then i tried this statement to check the timezone and curiously it was the right time.
SELECT db_user, extended_timestamp, action FROM v$xml_audit_trail WHERE db_user='CC' ORDER BY extended_timestamp DESC;
DB_USER EXTENDED_TIMESTAMP ACTION
CC 01.10.16 17:59:50,798000 +02:00 101
Do anyone know how to set the right timezone for the xml format?
Thanks in advance!

According to the documentation:
Extended_Timestamp indicates the time of the audited operation (timestamp of user login for entries created by AUDIT SESSION), in Coordinated Universal Time (UTC) or Greenwich Mean Time (GMT). This field only appears in the XML audit files, not the OS text audit files.
There doesn't seem to be anyway to change that, unless you transform the XML in the file later.
The timestamp is correct, it's just always in UTC. Whatever is going to consume this XML (you said you want to use it 'in your network') should be able to recognise that, and convert to other time zones if needed, but doing everything in UTC can be simpler anyway.
As an alternative you could generate your own XML from the v$xml_audit table with the data in whatever format(and timezone) you want, using the built-in XML generation tools (such as dbms_xmlgen).

Related

Two databases with the same data, are reporting different times

I live in Arizona, MST with no DST if it matters.
I have two different Oracle Databases, that is storing a list of holidays and weekends.
The column that stores the data is a DATE format, no time value.
But when I use the API to call the first db for holidays in Jan 2021, it reports the holiday date at midnight. But when I call for all holidays in Jan 2021 on the second db, it reports it as all holiday date as 7am.
The query that the db uses is
SELECT * FROM CLPUSER.HOL_DT WHERE TRIM(HOL_DT) >= ? AND TRIM(HOL_DT) <= ?";
The API is using Vertx-jdbc when calling the db. I have double checked the configuration of the API and the db, all are using MST.
Here are the values that are returned from the API call
DB1
"publicHolidayList": [
"holidayDate": "2021-01-01T00:00:00Z",
"holidayDate": "2021-01-02T00:00:00Z",
"holidayDate": "2021-01-03T00:00:00Z"
...
]
DB2
"publicHolidayList": [
"holidayDate": "2021-01-02T07:00:00Z",
"holidayDate": "2021-01-03T07:00:00Z"
...
"holidayDate": "2021-01-01T07:00:00Z" //appears at bottom
]
Edit: Just debugged through the code till I got to how vertx jdbc reads it from the underlining JDBC library, It is reading the HOL_DT from the jdbc as a TIMESTAMP, when the db stores it as a Date Format. When it does that, it converts to ZoneDateTime at UTC, giving its timevalue.
Turns out something was different. When redeploying the code in one the environments, it started to show the "2021-01-02T07:00:00Z" like the other. My working theory is that for some reason one of the pods was set to UTC time, but now at MST time.
Also, the whole one entry being out of order was due to it not having Sort by function, and when I inserted all of these in Oracle, I forgot the first entry and it got put to the end.
¯\_(ツ)_/¯

JPA + PostgreSQL: Only time being persisted

I have a PostgreSQL table containing a timestamp without time zone field. This is configured in my Java code as follows:
#Column(name = "timestamp")
#Temporal(TemporalType.TIMESTAMP)
private java.util.Date timestamp;
The timestamp property is being set to new Date() i.e. containing both the date and the time. However in the database the date part is not being persisted e.g. "14:40:28.889"; therefore when it is retrieved from the database the date is not loaded and set to default i.e. 01/01/1970. Any ideas where the date part of the timestamp is running off to?
UPDATE:
I increased the logging level for hibernate so I can see what is being persisted by adding the following in the application.properties file:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
The log output was as follows:
binding parameter [6] as [TIMESTAMP] - [Wed Sep 16 08:57:03 CEST 2015]
UPDATE 2:
I stepped through the code and found the insert sql with the date formatted as 2015-09-16 11:16:09.416000 +02:00:00. I insert the string automatically in PGAdmin and it insert with no error. After save a clicked the little refresh button and the date part disappeared! So the data is being persisted correctly but somehow postgre is doing something weird afterwards.
This was one of the silliest mistakes ever, I had a script that creates the tables and it was marked as "time without timezone" i.e. not timestamp so PostgreSQL was obviously removing the date. I will leave this question as it might help someone debug similar issues.

Oracle Date Literal [DD-MON-RR HH.MI.SSXFF AM]

I'm trying to use the timestamp format [DD-MON-RR HH.MI.SSXFF AM] to insert my date.
I can't modify the database settings in anyway possible, and I have to insert the date through a JAVA's string format (I can't modify the class that defined it either).
Having said that, I need to literally reconstruct the format string-by-string without tempering the other class/db.
The nls settings for date is DD-MON-RR. 12-JUN-2012 and 12/JUN/2012 worked perfectly fine.
But I find it difficult to recreate the timestamp part of the date.
Listed below is a few format I've tried.
'12-JAN-12' < success
'12/JAN/2012' < success
'12/JAN/2012 10.30.25.000 AM < failed
'12/JAN/2012 10:30:25.000 AM < failed
Did messed up the : or .? Or was the zero(s) aren't enough for miliseconds? Been in this trouble for hours now.
Thanks in advance.
EDIT
After a few reasoning sessions, the seniors gave their permission to alter the model class. Everything's good now. Thanks for the help and suggestions.
Since you must send a string to the DB, you must rely on the implicit conversion of the oracle DB to the DATE type.
Since you can't change the DB settings, the only thing I can suggest is changing the session settings.
So, if you can run commands against the DB, try:
Statement st = conn.createStatement();
st.execute("alter session set NLS_DATE_FORMAT='dd/mm/yyyy hh24:mi:ss'");
(or some other format (it's not recomended to use mon in your format because it might involve NLS_TERRITORY too))

lotus notes search by date with Java api

I'm trying to select records by date from a Lotus Notes database and have run into trouble with correctly formatting the date.
Here's the relevant code:
public void runNotes() {
Session s;
try {
s = NotesFactory.createSession((String)null, (String)null, "mypassword");
Database hkDB =
s.getDatabase("NBHDH001/YNM", "H\\DHH00001.nsf", false);
DocumentCollection docs = hkDB.search("[Date]>[2012/03/20]");
Date is a field in the record, and when I looked up records (with FTSearch), the date came back in the format above: [yyyy/mm/dd].
The parameter of the search is what I need here.
i.e. what should I put instead of "[Date]>[2012/03/20]"
I tried various constructions with Calendar and DateFormat, but it's not coming together...
Any suggestions?
You should get rid of the square brackets on the field name. The search method expects a Notes Formula, like what you'd put into a view selection formula:
"Date > [03/20/2012]"
It might also be required that dates are in mm/dd/yyyy format, though if you are in a non-US locale I'm not 100% sure.
You mentioned that you have been doing full text searches in the database, so it is definitely worth mentioning this... If the database actually has a full text index, then you may want to consider using the NotesDatabase.FTSearch() method instead of NotesDatabase.Search(). The FTSearch method will be considerably faster for a large database.
The syntax for FTSearch is different from the syntax for Search. You could use either "FIELD Date > 03/20/2012" or "[Date] > 03/20/2012".

How to send a Timestamp field to Oracle stored proc. from Java despite the DB config?

I'm making a request from a java webapp to an Oracle' stored procedure which happens to have a Timestamp IN parameter.
The way info travels is something like:
javaWebApp --} webservice client --} ws --} storedProcedure
And I send the Timestamp param as a formatted string from the webservice client to the ws.
In the testing environment, it works sending:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
input.setTimestampField(dateFormat.format(new Date()));
As you see, a formatted string is sent. But in the production environment, it raises an exception
ORA-01830: date format picture ends before converting entire input string.
It relates to the format not being the same, possibly due to differences in configuration from one DB to the other. I know the testing environment should be a replica of the production site, but it is not in my hands to set them properly. And I need to send the Timestamp-as-a-formatted-string field despite the way they setup the database. Any ideas? Thanks in advance.
**** EDIT ****: I've found the way to make it work properly despite the particular configuration. It is as simple as setting the call instruction in the web service with the appropiate Oracle instructions. I mean, the calling to the Oracle stored procedure went from
"call PACKAGE.MYPROCEDURE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
to
"call PACKAGE.MYPROCEDURE(?,?,?,?,?,?,TO_TIMESTAMP(?, 'DD-MM-YYYY HH24:MI:SS'),?,?,?,?,?,?,?,?,?,?,?)"
while the format I set in the procedure calling matches the format sent by the webapp using the SimpleDateFormat stated in the original question, slightly modified:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Thank you all for the help and the ideas.
The default NLS_DATE_FORMAT generally doesn't include the time and only a two-digit year. It is probably either DD-MM-YY or MM-DD-YY.
If the WS receives a string and the database stored procedure needs a timestamp, then the two of them will need to negotiate the format mask. Either the WS, when it connects to the database, should set an explicit date format, or the database should be able to accept a string and convert it using a hard-coded format.
Unless there is some particular negotiation you have defined in the WS, nothing the JavaWebApp or WebServiceClient will be able to influence the format that the database assumes the WS is using.
All that said, I'd have a look around any other code at your end and see if there's anything doing a similar translation. You may find something else using a specific format.
What does your query look like in the input prepared statement? That error indicates that Oracle doesn't like the date format you have passed in. Your test environment may have a different NLS_DATE_FORMAT set on the database or machine/driver being used.

Categories