Bo's blog

Thursday, August 19, 2010

Locks in Oracle

copied from (http://www.adp-gmbh.ch/ora/concepts/lock.html)


Locks in Oracle



create table lck (a number, b number);

insert into lck values (1,2);
insert into lck values (2,4);
insert into lck values (3,6);
insert into lck values (4,8);
insert into lck values (5,3);
insert into lck values (6,5);
insert into lck values (7,7);

commit;


We use two sessions (distinguishable by two colors and being on the left or right side) to investigate Statement-level read consistency, and a third to select from v$lock



First, we find the session id of the two participating sessions:



SQL> select sid from v$session where audsid=userenv('SESSIONID');

SID
----------
14


SQL>select sid from v$session where audsid=userenv('SESSIONID');

SID
----------
10


Now, we're inserting a row in the first session (sid=14).



SQL> insert into lck values (1000,1001);

1 row created.

SQL> select * from lck;

A B
---------- ----------
1 2
2 4
3 6
4 8
5 3
6 5
7 7
1000 1001

8 rows selected.


How does that influence v$lock?



SQL> select sid,type,id1,lmode,request from v$lock where sid in (10,14);

SID TY ID1 LMODE REQUEST
---------- -- ---------- ---------- ----------
14 TX 262153 6 0
14 TM 4145 3 0


Session 14 (the one that inserted a row) has obviously aquired two locks (request = 0). One of these locks is a Transaction Lock (type=TX), the other is a DML or Table Lock (type=TM). Mode 3 means: Row Exclusive which acutally makes sense. Now, we can use obj$ to verify if the TM Lock is indeed put on the table LCK:



SQL> select name from sys.obj$ where obj# = 4145 ;

NAME
------------------------------
LCK


What does the 2nd session see if it queries LCK?



SQL>select * from lck;

A B
---------- ----------
1 2
2 4
3 6
4 8
5 3
6 5
7 7

7 rows selected.


The 1st session has not yet commited (or rollbacked) its session, so the changes are not visible to other sessions.



Now, let's have the first session insert another row.



SQL> insert into lck values (1001,1000);

1 row created.


We'd expect v$lock to have an row more (for this 2nd inserted row). But never believe your feelings...



SQL> select sid,type,id1,lmode,request from v$lock where sid in (10,14);

SID TY ID1 LMODE REQUEST
---------- -- ---------- ---------- ----------
14 TX 262153 6 0
14 TM 4145 3 0


Didn't much change, did it? Now, the 2nd session updates a row:



SQL>update lck set a=2000,b=2001 where a=1;

1 row updated.

SQL>select * from lck;

A B
---------- ----------
2000 2001
2 4
3 6
4 8
5 3
6 5
7 7

7 rows selected.


And v$lock?



SQL> select sid,type,id1,lmode,request from v$lock where sid in (10,14);

SID TY ID1 LMODE REQUEST
---------- -- ---------- ---------- ----------
10 TX 327698 6 0
10 TM 4145 3 0
14 TX 262153 6 0
14 TM 4145 3 0


SQL>insert into lck values (2001,2000);

1 row created.

SQL>select * from lck;

A B
---------- ----------
2000 2001
2 4
3 6
4 8
5 3
6 5
7 7
2001 2000

8 rows selected.


What happens, if the first session wants to update a row that was already updated (but not yet commited) by another session? The first session tries to do exactly that (the row in which a=1)



SQL> select * from lck;

A B
---------- ----------
1 2
2 4
3 6
4 8
5 3
6 5
7 7
1000 1001
1001 1000

9 rows selected.

SQL> update lck set a=1002,b=1003 where a=1;


The session hangs until the session that has put a lock on the row in question commits (or rollbacks). This waiting is recorded in v$session_wait:



SQL> select event, seconds_in_wait, sid from v$session_wait where sid in (10,14);

EVENT SECONDS_IN_WAIT SID
---------------------------------------------------------------- --------------- ----------
enqueue 1593 14
SQL*Net message from client 2862 10


v$session_wait tells even how long (in secondes) the session waited. Now, let the 2nd session commit:



0 rows updated.

SQL>


SQL>commit;

Commit complete.


Note, this can be confusing. When the 1st session selected * from lck, it defenitively saw a row where a=1 but after seemingly updating it, it wasn't actually updated.



What do these sessions now see, if they both do a select *?



SQL> select * from lck;

A B
---------- ----------
2000 2001
2 4
3 6
4 8
5 3
6 5
7 7
1000 1001
1001 1000
2001 2000


SQL>select * from lck;

A B
---------- ----------
2000 2001
2 4
3 6
4 8
5 3
6 5
7 7
2001 2000


While the 1st session sees, what the 2nd session commited, the 2nd session does not see the uncommited changes of the first session. If the 1st session had been Transaction-level read consistent, it would not see any changes until it commits.

[72000] ORA-01591: lock held by in-doubt distributed transaction 1.65.465

Check the dba_2pc_pending view
If there are in-doubt distributed transaction, you'l see them there.
Oracle's doc has as whole section on "Managing Distributed Transactions"
There is a procedured documented there "dbms_transactioin.purge_lost_db_entry"
That will clean these transactions up.

select * from dba_2pc_pending view will see

local_trans_id

1.65.465

 

SYS.PENDING_TRANS$, SYS.PENDING_SESSIONS$

 

login as sysdba and execute the proc

export ORACLE_SID=main
sqlplus / as sysdba

execute dbms_transactioin.purge_lost_db_entry;

 

Sometimes, it is not possible because you do not have rows in DBA_2PC_PENDING view. If this is the case, you can insert dummy rows into SYS.PENDING_TRANS$, and SYS.PENDING_SESSIONS$ tables to correct this. Below is a script that can help you if you do not have rows in DBA_2PC_PENDING view. Just enter the TRANSACTION ID, got from ORA-1591 error or from alertlog files.


--
-- Execute it connected as SYS or SYSDBA
--
ACCEPT TRANSACTION_ID PROMPT "Enter TRANSACTION ID: "
alter system disable distributed recovery;
insert into pending_trans$ (
LOCAL_TRAN_ID,
GLOBAL_TRAN_FMT,
GLOBAL_ORACLE_ID,
STATE,
STATUS,
SESSION_VECTOR,
RECO_VECTOR,
TYPE#,
FAIL_TIME,
RECO_TIME)
values( '&&TRANSACTION_ID',
306206, /* */
'XXXXXXX.12345.1.2.3', /* These values can be used without any */
'prepared','P', /* modification. Most of the values are */
hextoraw( '00000001' ), /* constant. */
hextoraw( '00000000' ), /* */
0, sysdate, sysdate );
insert into pending_sessions$
values( '&&TRANSACTION_ID',
1, hextoraw('05004F003A1500000104'),
'C', 0, 30258592, '',
146
);
commit;
rollback force '&&TRANSACTION_ID'; /* or commit force */
commit;
alter system enable distributed recovery;
exec dbms_transaction.purge_lost_db_entry( '&&TRANSACTION_ID' );
commit;
delete from pending_trans$ where local_tran_id='&&TRANSACTION_ID';
delete from pending_sessions$ where local_tran_id='&&TRANSACTION_ID';
commit;

Oracle alert system

(Copied from http://www.adp-gmbh.ch/ora/sql/alter_system.html)

Alter system archive log [start|stop|all|...]

alter system archive log all;
alter system archive log next;
alter system archive log sequence 104;
alter system archive log current;
alter system archive log current noswitch;


The following command can be used to stop arch.



alter system archive log stop


Similarly, arch is started with



alter system archive log start


However, changing the archiver this way doesn't last when the database is restarted. When the database is started, it consults log_archive_start in the initialization file to determine if arch is started.



Alter system archive log all


This command is used to manually archive redo logs.



alter system disconnect session



alter system kill session



alter system kill session 'session-id,session-serial'


This command kills a session. The session-id and session-serialparameters are found in the v$session view (columns sid andserial#.



alter system checkpoint



Performs a checkpoint



alter system checkpoint


alter system dump datafile



This command can be used in order to dump one ore more blocks of a datafile. The following command dumps blocks 50 through 55 of file 5. Which file 5 is can be found out withv$datafile



alter system dump datafile 5 block min 50 block max 55;


Note: trace files are only readable by the Oracle account. If you want to change this, set the undocumented initialization parameter _trace_files_public to true. Doing so will, however, cause a big security risk.



alter system flush buffer_cache



alter system flush buffer_cache;


This command is not available prior to 10g. It flushes the buffer cache in the SGA.



9i had an undocumented command to flush the buffer cache:



alter session set events = 'immediate trace name flush_cache';


alter system flush shared_pool



alter system flush shared_pool;


This command flushed the shared pool.



alter system quiesce restricted



alter system suspend|resume



alter system switch logfile



Causes a redo log switch.



alter system switch logfile;


If the database is in archive log mode, but the ARCH process hasn't been startedm, the command might hang, because it waits for the archiving of the 'next' online redo log.



alter system register



Forces the registration of database information with thelistener.



alter system register


Alter system set timed_statistics



Setting timed_statistics=true might be usefule when using tk prof.



Alter system set sql_trace



Setting sql_trace=true is a prerequisite when using tk prof.



Alter system set .... deferred



Alter system can be used to change initialization parameterson system level. However, some parameters, when changed with alter system don't affect sessions that are already opened at the time when the statement is executet; it only affects sessions started later. These parameters must be changed with alter system set <initialization parameter> DEFERRED, otherwise a ORA-02096: specified initialization parameter is not modifiable with this option error is returned.



These parameters can be identified as they have a DEFERRED in the isses_modifiable column of v$parameter.



Alter system reset <parameter_name>



Resets a parameter.



alter system reset some_param scope=both sid='*';


scope



scope=memory


Changes the parameter's value for the running instance only. As soon as the instance is stopped and started, this change will be lost.



scope=spfile


Alters an initialization parameter in the spfile



scope=both


Alters an initialization parameter in the spfile as well as in the running instance.

Sunday, August 15, 2010

Joomla module depend on the language

Let's assume we offer our content in 2 languages, English and French. We open up our template's index.php file and locate where this change should occur, e.g. in the place of a current module position:

Code:

<?php mosLoadModules ('banner_en',-1); ?>

We change this to the following:

Code:

<?php
if ($mosConfig_lang == 'english' ) {
mosLoadModules ('banner_en',-1);
} else if {
if ($mosConfig_lang == 'french' ) {
mosLoadModules ('banner_fr',-1);
} else { // default - if no language is defined for some reason
mosLoadModules ('banner_en',-1);
}
?>

Sunday, August 08, 2010

Using git with SVN repo

 
  1. initialize the git repo. 
    • mkdir git-repo
    • cd git-repo
    • git init
  2. init git-svn repo. it will create a section [svn-remote “svn”] into file git-repo/.git/config
    • for local svn, use the following command or use can manually add the section to git-repo/.git/config
        The colors in purple is must match to your remote SVN server, and colors in fuchsia means the name of remote branch will be created in git repo.
        [svn-remote "svn"]
            url = file:///cygdrive/d/work/000-repos
            fetch = projectName/trunk:refs/remotes/trunk
            branches = projectName/branches/*:refs/remotes/*
            tags = projectName/tags/*:refs/remotes/tags/*
    • for remote svn, use the following command
    • [option] add --prefix projectName/ can add the projectName to refs if you want to access multiple projects
      • git svn init https://remote.com/svn --prefix projectName/ -T projectName/trunk -b projectName/branches -t projectName/tags

      [svn-remote "svn"]
          url = file:///cygdrive/w/enreach/000-repos
          fetch = projectName/trunk:refs/remotes/projectName/trunk
          branches = projectName/branches/*:refs/remotes/projectName/*
          tags = projectName/tags/*:refs/remotes/projectName/tags/*

  3. fetch the revision history from svn
    • git svn fetch
  4. check the remote branch(SVN) after fetch is done.
    • git branch –r

        rel1.1
        bug-fix
        trunk

  5. create a local branch for the remote SVN branch you want to work on
    • git checkout master
    • git checkout –b git-bug-fix bugfix
    • git svn rebase
  6. clone another local git to work and keep the git-repo to sync with the remote SVN server.
    • git checkout git-bug-fix
    • git clone . ../git-local
  7. work on the git-local,
    • cd ../git-local
    • edit some files;
    • git commit –a;
    • edit some files;
    • git commit –a
    • git status# On branch git-bug-fix
      # Your branch is ahead of 'origin/git-bug-fix' by 2 commits.
  8. now if you want to push the changes to remote svn server.
    • cd ../git-repo
    • git checkout git-bug-fix
    • git svn rebase
    • git pull ../git-local (this will get all commits in to branch git-bug-fix, keep all commits individual)
    • if you want to merge all small commits into one, use the following command
      • git pull --squash ../git-local (this will get all commits into branch git-bug-fix as one commit)
    • git status

      windows/config.h: needs merge
      # On branch git-bug-fix
      # Changes to be committed:
      #   (use "git reset HEAD <file>..." to unstage)
      #
      #       modified:   test/test.c
      #
      # Changed but not updated:
      #   (use "git add <file>..." to update what will be committed)
      #   (use "git checkout -- <file>..." to discard changes in working directory)
      #
      #       unmerged:   windows/config.h
      #

    • if there has any conflict like above, then fix it and use
      • git add windows/config.h
    • git commit –m “blah, blah…”
    • git svn dcommit

Monday, August 02, 2010

CSS & Joomla useful sites

joomla test:

http://docs.joomla.org/Unit_Testing_--_a_Simple_Example

http://docs.joomla.org/Running_Automated_Tests_for_Version_1.6

http://www.siimviikman.com/tag/joomla/ – Joomla with Doctrine

http://www.rockettheme.com/ Joomla themes

http://www.shinylight.com/2009/10/03/ant-script-to-deploy/

960:

http://960.gs/
http://www.runningskull.com/2008/working-with-960gs/

http://csscreator.com/

http://www.positioniseverything.net/easyclearing.html

http://perishablepress.com/press/category/websites/