Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Wednesday, August 24, 2016

Check DB connection to an Oracle DBMS from a remote machine

Using SQLPLUS:

Execute following command in a command line:
sqlplus "<DB_USERNAME>/<DB_PASSWORD>@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<HOST_NAME>)(Port=1521))(CONNECT_DATA=(SID=<SID>)))"


You will be able to login to the database in the remote machine, if connection is successful.

Using Weblogic DBPing utility

Execute following command in a command line:
java -classpath <WL_HOME>/<WL_SERVER>/server/lib/weblogic.jar utils.dbping ORACLE_THIN <DB_USERNAME> <DB_PASSWORD> <HOST_NAME>:<DB_PORT>:<SID>

You will get following message if connection is successful.
**** Success!!! ****

You can connect to the database in your app using:

  java.util.Properties props = new java.util.Properties();
  props.put("user", "<DB_USERNAME>");
  props.put("password", "<DB_PASSWORD>");
  java.sql.Driver d =
    Class.forName("oracle.jdbc.OracleDriver").newInstance();
  java.sql.Connection conn =
    Driver.connect("jdbc:oracle:thin:@<HOST_NAME>:<DB_PORT>:<SID>", props);




References:

  •  https://docs.oracle.com/cd/E13222_01/wls/docs/techstart/dbping.html
  • http://javaeesupportpatterns.blogspot.sg/2011/03/network-adapter-could-not-establish.html
  • http://dba.stackexchange.com/questions/13075/how-to-use-sqlplus-to-connect-to-an-oracle-database-located-on-another-host-with

Saturday, April 2, 2016

Using Tkprof tool for Analysing Oracle trace files

What is TkProf?

TkProf is an Oracle tool which helps to convert Oracle trace files to human readable format.

Where to find the tool?

TkProf tool is located in %ORACLE_HOME%/bin folder.

How to use it?

0. login to SQLPlus.
1. First find the trace file dump folder using following sql command.
select value from v$parameter where name = 'user_dump_dest';
The output:
/<PATH TO YOUR DB>/<YOUR_SID>/trace
2. Then enable the sqltrace (This can affect to performance).
alter session set sql_trace=true;

3. Run your sql.
4. Check the trace file by doing a "ls -ltr | grep _ora_". You can run a shell command within SQLPlus by prepending "!" to the command.
SQL> !ls -ltr /<PATH TO YOUR DB>/trace | grep _ora_
-rw-rw---- 1 oracleuser dbagroup     1386 Sep 28 14:30 YOUR_SID_ora_27758.trm
-rw-rw---- 1 oracleuser dbagroup     3628 Sep 28 14:30 YOUR_SID_ora_27758.trc
-rw-rw---- 1 oracleuser dbagroup     1359 Sep 28 14:55 YOUR_SID_ora_29733.trm
-rw-rw---- 1 oracleuser dbagroup     3779 Sep 28 14:55 YOUR_SID_ora_29733.trc
-rw-rw---- 1 oracleuser dbagroup     1355 Oct 21 16:07 YOUR_SID_ora_32487.trm
-rw-rw---- 1 oracleuser dbagroup     3613 Oct 21 16:07 YOUR_SID_ora_32487.trc
-rw-rw---- 1 oracleuser dbagroup     122 Oct 25 14:38 YOUR_SID_ora_6253.trm
-rw-rw---- 1 oracleuser dbagroup     3506 Oct 25 14:38 YOUR_SID_ora_6253.trc
5. The last updated ".trc" file of above output is the one related to our SQL query analysis.
6. Run the tkprof command to get the formatted result. The tkprof tool is located at bin folder of %ORACLE_HOME% folder.
/home/oracleuser[YOUR_SID]:tkprof  /<PATH TO YOUR DB>/YOUR_SID/trace/YOUR_SID_ora_6253.trc  tkprof_out.txt
TKPROF: Release 11.2.0.3.0 - Development on Sun Oct 25 14:41:58 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
/home/oracleuser[YOUR_SID]:less tkprof_out_3.txt

More Details:

Please see following websites for more detailed information about how to use TkProf tool and analysing the output.

Example output of TkProf:

TKPROF: Release 11.2.0.3.0 - Development on Sun Oct 25 14:41:58 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Trace file: /<PATH TO YOUR DB>/YOUR_SID/trace/<YOUR_SID>_ora_6253.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

SQL ID: 4tk6t8tfsfqbf Plan Hash: 0

alter session set sql_trace=true


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.03          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.03          0          0          0           0

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: CHOOSE
Parsing user id: SYS
********************************************************************************

<YOUR_SQL_QUERY>

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        1      0.01       0.01          0         53          1           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        3      0.01       0.01          0         53          1           0

Misses in library cache during parse: 1
Optimizer mode: CHOOSE
Parsing user id: SYS
Number of plan statistics captured: 1

Rows (1st) Rows (avg) Rows (max)  Row Source Operation
---------- ---------- ----------  ---------------------------------------------------
         0          0          0  NESTED LOOPS  (cr=53 pr=0 pw=0 time=14025 us)
         0          0          0   NESTED LOOPS  (cr=53 pr=0 pw=0 time=14018 us cost=12 size=162 card=1)
      2502       2502       2502    PARTITION HASH SINGLE PARTITION: 1 1 (cr=53 pr=0 pw=0 time=10393 us cost=12 size=170136 card=2502)
      2502       2502       2502     TABLE ACCESS FULL <YOUR_TABLE_1> PARTITION: 1 1 (cr=53 pr=0 pw=0 time=5007 us cost=12 size=170136 card=2502)
         0          0          0    PARTITION HASH SINGLE PARTITION: KEY KEY (cr=0 pr=0 pw=0 time=6275 us cost=0 size=0 card=1)
         0          0          0     INDEX UNIQUE SCAN <A_PK_INDEX> PARTITION: KEY KEY (cr=0 pr=0 pw=0 time=0 us cost=0 size=0 card=1)(object id 15488)
         0          0          0   TABLE ACCESS BY LOCAL INDEX ROWID <ANOTHER_TABLE_2> PARTITION: 1 1 (cr=0 pr=0 pw=0 time=0 us cost=0 size=94 card=1)




********************************************************************************

********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      2      0.00       0.03          0          0          0           0
Fetch        1      0.01       0.01          0         53          1           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.01       0.05          0         53          1           0

Misses in library cache during parse: 1
Misses in library cache during execute: 1


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      0      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        0      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0

    2  user  SQL statements in session.
    0  internal SQL statements in session.
    2  SQL statements in session.
********************************************************************************
Trace file: /<PATH TO YOUR DB>/YOUR_SID/trace/<YOUR_SID>_ora_6253.trc
Trace file compatibility: 11.1.0.7
Sort options: default

       1  session in tracefile.
       2  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
       2  SQL statements in trace file.
       2  unique SQL statements in trace file.
      59  lines in trace file.
      78  elapsed seconds in trace file.

 

Friday, February 12, 2016

Tips on Tools Usage - SqlPlus

Editing a previously executed sql statement:

Type ed in the sqlplus. Then sqlplus will open the previous command in an editor (e.g: in vi). Then you can edit the sql command and save it using Esc key + :wq as you normally does in "vi" editor.
Then type / to run the edited command.

Executing the previous sql statement:

Type / or r in the sqlplus terminal to run the previously executed sql statement again.

Show the previously executed sql statement without executing it:

Type "list" in the sqlplus terminal.

Formatting of the sql output:

Change the width of a column in the sql output, for a character field:

column <filed_name> format a<width_number_of_characters>

E.g:
column user_name format a100

Change the width of a column in the sql output, for a numeric field:

column <field_name> format <number_format>

E.g:
column salary format 9999.99

Set the line size of the sql output:

set linesize <line_size>

E.g:
set linesize 300

More Details:

http://www.comp.nus.edu.sg/~ooibc/courses/sql/sqlplus.htm
http://ugweb.cs.ualberta.ca/~c391/manual/chapt4.html
http://www.orafaq.com/wiki/SQL*Plus

Tuesday, October 20, 2015

Oracle: Finding the value for "log_history" in DBMS_SCHEDULER

In Oracle, The "log_history" attribute in DBMS_SCHEDULER is used to specify the number of days  of log history to keep.
The default value of this attribute is 30 days. Scheduler purges the logs (job logs and windows logs) which are older than "log_history" number of days, once per day.

Execute following anonymous block in sqlplus to find the current value of 'log_history" attribute.

set serveroutput on
DECLARE
  val VARCHAR2(100);
BEGIN
  dbms_scheduler.get_scheduler_attribute('log_history', val);
  dbms_output.put_line(val);
END;
/

To change the "log_history" value, you can use
dbms_scheduler.set_scheduler_attribute('log_history','40');

Tuesday, November 13, 2012

A JPA issue which was caused by the Oracle DB's treatment of empty strings in VARCHAR fileds as NULL

Yesterday I encountered a strange issue when I was doing a performance test of my current project using JMeter. My project uses EJB 3 on Weblogic 10.3 server and an Oracle 11g database. I executed a JMeter test with 25 threads with 4000 iterations. But on each test execution, the test cases failed with a UniqueConstraintException after running the test cases for about 10 minutes. 

That unique constraint violation exception happened because system was trying to insert a record which was already inserted to the database in setUpTest() method in JMeter java sampler client. But this was not supposed to happen because in actual test cases (in runTest() methods), the system runs a find query to check whether a record with matching value is already existing in the database before inserting any record of the specific type to the database. Another strange thing was this happened only to the test cases executed after the first 10 minutes of the test.

So after some investigation, I found that in the setUpTest() method, one field was set to an empty string in the specific entity object (record) I mentioned earlier. But when I checked the relevant record in the Oracle database, it contained NULL value instead of an empty string for the relevant field. 

After some googling, it turned out that when JPA (or using normal SQL query) insert a empty string to Oracle database, Oracle converts it to a NULL before storing it in the database. 

So the reason behind my issue is when JPA insert an entity object which has an empty string as the value for a its property, Oracle changes that field value to NULL and store NULL value in the database instead of an empty string. But in the JPA cache, the value of the relevant property of the inserted object is still an empty string, not a NULL value. When system runs the JPQL find query, it checks for the relevant object in the cache first. Because the relevant newly inserted object still has an empty string for the value of the relevant property, the find query returns that object, so system doesn't try to create a new object with the same values again. Therefore we don't get an unique constraint violation exception. But after about 10 minutes, the JPA cache expires and cache is reloaded from the database. At this time, the value for the previously mentioned field of the reloaded specific object is no longer an empty string, because what is in the database for that field is NULL. So from now on, when the system tries to find an object with a value of empty string for this field, it is no longer able to find it. Therefore it assumes this record doesn't exist in the database and tries to re-insert the record.  But because Oracle changes the empty string to NULL when it inserts the relevant record to the database and because a record with NULL already exists in the database, it throws an unique constraint violation exception.

See this discussion in stackoverflow for more details on why Oracle treats VARCHAR empty strings as NULL.