Showing posts with label DataBase. Show all posts
Showing posts with label DataBase. Show all posts

Database table sequence generation and retrieval in MySQL & DB2


To insert a record in to database table “A” (where auto generated sequence number is primary key) and retrieve that sequence no and insert a record into table B ( where foreign key is referring to table “A” primary key) .

Ex:
CREATE TABLE A (
               priColumn INT NOT NULL,
               genColumn VARCHAR(20),
               PRIMARY KEY(priColumn)
               );

CREATE TABLE B (
               priColumn INT NOT NULL,
               genColumn VARCHAR(30),
               FOREIGN KEY (priColumn) REFERENCES A(priColumn)
               );

To handle this scenario in MySQL
1.      To generate the sequence number in MySQL , you no need to execute separate DDL’s like “create sequence” ..etc, instead you can use “AUTO_INCREMENT” on table ddl
CREATE TABLE A (
        priColumn INT NOT NULL AUTO_INCREMENT,
        genColumn VARCHAR(20),
        PRIMARY KEY(priColumn)
        );

2.      Every time you insert record into table A, priColumn is filled automatically.

3.      Use the following select statement to retrieve sequence number (Auto Increment) value generated after insert statement .
SELECT LAST_INSERT_ID()


To handle this scenario in DB2

1.      There are different ways you can generate the unique values in the DB2.
a.      Using “generate_unique()” function
INSERT INTO A VALUES (GENERATE_UNIQUE(), 'general column1')
b.      Using Identity column
CREATE TABLE A (
               priColumn INT NOT NULL GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1, NO CACHE),
               genColumn VARCHAR(20),
               CONSTRAINT X_priColumn PRIMARY KEY(priColumn)
);
                             
               You can simply insert row like and generates unique value in the “priColumn”
INSERT INTO A VALUES (‘general column1');


2.      To retrieve the value inserted in using identity column, DB2 has function called IDENTITY_VAL_LOCAL , it retrieves the last inserted sequence id.

You can now retrieve the last inserted value in table A and put in table B ,
INSERT INTO B VALUES (identity_val_local(), ‘general column1');



Resources



Creating MySQL datasource on Websphere Application Server


1.      Create the J2C Authentication alias with MySQL username and password

2.      Creating JDBC provider,
a.      choose the following values

Database type                   : user-defined
Implementation class name :  com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource


b.      In next step provide MySQL JDBC driver jar file ( While installing the MySQL community server , make sure to install java connector (connector J))


c.       Click finish



3.      Once JDBC provider is created , you can create the new datasource under that JDBC provider
a.      Enter datasource name


b.      Enter data store helper class name
Data store helper class name :: com.ibm.websphere.rsadapter.GenericDataStoreHelper


c.       Select the container-managed authentication alias and choose the J2C authentication alias that’s created at starting.


d.      Once Datasource created, need to adjust the following custom properties
Update the following five custom properties


databaseName :test
user                  :root
password         :Passw0rd
port                  :3306
serverName      :sivapc.sivavaka.com







MySQL Workbench


Along with installation of the MySQL community server , MySQL workbench also got installed and it found it much useful to administer the DB , data modeling and also for the development purposes (DDL’s  , DML’s)



Easy to start/stop the MySQL server now from server administration




By default there are few schemas and tables has already created as part of the installation


MySQL Installation

As part of testing spring portlet application locally,  I had to install MySQL in my local workstation and captured the following details while installation.

1.      Download MySQL community server for your operating system ( these screens are captured based on Windows 7 64-bit )




2.      Click on “download” to download “mysql-installer-community-5.5.29.0” (this version is when I downloaded)

3.      Once download is finished , Run installer.





4.      Choose installation type “Full”


It asked me to install the Visual Studio 2010 tools for office runtime.




5.      I have chosen  “Development Machine” option and default TCP/IP enabled



6.      Specify the MySQL Root password



7.      And I have added admin user (in addition to root, this step is not mandatory)




8.      Select to “run as the windows service”



9.      Specify log file location






Cloning JCR repository insteady of syndication or import and export WCM libraries

Syndicating items from one server to another, either after migration or to roll out a new WCM environment can take a long time. Your database backup and restore features can be used to speed up the initial set up and clone data from one repository to another. This makes your system ready for syndication to be used from then on for incremental updates.

Cloning JCR repositoy will be another approach in migration projects to switch from existing system to target WCM system.This will reduce the production switch time dramatically.

Click here to download the instructions for cloning WCM repository from IBM.

Installing Tivoli Directory Server 6.2

Steps to install Tivoli Directory Server 6.2 on Windows

Assumptions:
a). DB2 is already installed
b). Websphere Portal or Applicaiton Server is already installed. This is not mandatory, required only when you want use the tivoli web admin console


1. Select features want to install

If you want to install the Tivoli Web Admin Console on existing WAS install , don't select the embeded WAS.


2.  Click Next,  There you will have different options
     a) Create proxy directory server (If you want to use this as Proxy server between multiple LDAP's)   

     b) Create default directory server

Click this button if you want to create a new directory server instance with default settings. The default instance will be named dsrdbm01, and a user name of dsrdbm01 will be created on the system if it does not exist. The administrator DN for the instance will be 'cn=root'. The database instance name will be dsrdbm01 and the database name will be dsrdbm01. You will be asked to provide a password for the user, a password for the administrator DN, and an encryption seed , salt for the directory server instance.
TDS instance: dsrdbm01  / pwd
TDS user       : dsrdbm01  / pwd
Admin DN    : cn=root    / pwd
DB instance   : dsrdbm01
DB Name       : dsrdbm01
Encryption      : encryption-abcdefghi
Encryption salt: abcdefg-salt (exactly 12 chars)

      c) Create new directory server with custom settings


Enter the directory server instance name (It will create the OS user ,if its not there already), Enter bother Encryption Seed String and Salt (below image doesn't show the the seed value, but make sure to enter both).

3. Click Next to create the DB2 database instance





4. Click Next to create the LDAP admin user
5. Click Next , It will shows the default ports , no need to change anything
6. Click Finish in final summary page



Troubleshooting Information

Following errors I came across while installing TDS


Problem 1: Stash Key Problems
Solution: Please make sure to enter the "encryption salt string"


Problem 2: Problems creating the DB2 instance
Solution: Make sure to login as local user(created by DB2) who is a member of local Administrators, DB2ADMNS groups, and if you logged in as the Domain user It doesn’t work while installing.

Note: Extension to this problem some time you may need to run the following DB2 command to create the DB2ADMNS and DB2USERS group in local. If the DB2USERS group is not found, extended security for DB2 on Windows might not be enabled. To enable extended security on Windows, stop the database, run the db2extsec.exe command, and then restart the database again as shown.

Db2stop
Db2extsec
Db2start

NOTE : 
1. If any problem you can delete the TDS instance and corresponding the DB2 instance using the TDS adminstration console, and re-create using the above steps
2. Once the installation done, you should able to see new DB2 instance , to list the DB2 instances use following cmd


list all db2 instances :> db2ilist




TDS - Web Administration Tool

Copy the IDSWebApp.war file from the TDS installer and install this on the WAS console as any other war file and you can access using URL like below

http://localhost:9082/IDSWebApp/IDSjsp/IDSConsoleFrameWork.jsp

Use the default UserID/Pwd : superadmin/ secret
 

Derby DB basic information

Few Details about the Derby Database

Standalone Derby Database


Click Here to download Derby DB(10.1.3.1). No installation is required


Starting derby DB

from Lib(C:\db-derby\lib)
java -jar derbynet.jar start

from framework/bin(C:\db-derby\frameworks\NetworkServer\bin)
startNetworkServer.bat

Few commonly used  SQL statements

CREATE SCHEMA sivavakablog
CREATE TABLE sivavakablog.reader
       (READER_ID int NOT NULL,
        REGISTERED_DATE DATE NOT NULL,
        READER_NAME CHAR(30),
        PRIMARY KEY (READER_ID));

insert into sivavakablog.reader(reader_id,registered_date,reader_name) values (1111,'2010-11-11','reader1');

update sivavakablog.reader set reader_name='reader2' where reader_id=1111

drop table sivavakablog.reader

select * from sivavakablog.reader;

drop index release.IX3000A

CREATE INDEX JCR.IX2110B ON JCR.PROT_RES (OWNER_UID ASC, RES_TYPE ASC);


Ref Complete SQL References:
http://db.apache.org/derby/manuals/reference/sqlj27.html#HDRSII-SQLJ-31580 (Create Commands)
http://db.apache.org/derby/papers/JDBCImplementation.html (JDBC related)
http://db.apache.org/derby/manuals/reference/sqlj28.html  (Drop commands)


Embed Derby Server (Websphere Portal)

  1. Open command prompt and go to \IBM\WebSphere\AppServer\derby\bin\embedded\
  2. enter systeminfo.bat to see what are the environment variables
  3. If Linux , check the current classpath using below and append as follow,
    1. echo $classpath
  4. If windows, check the current class using below
    1. echo %classpath%
    2. set classpath=%classpath%;
  5. if the JDBC client is not in classpath , set that as below
  6. open the ij.bat
Also consider that the version of Derby provided with WebSphere Portal has stress and failover limitations that are expected to be resolved in a later release of Derby performance improvements.

connect 'jdbc:derby://localhost:1527/wpsdb;user=wpsadmin;password=password'

D:/IBM/WebSphere/AppServer/derby/lib/derby.jar;D:/IBM/WebSphere/AppServer/derby/lib/derbyclient.jar;D:/IBM/WebSphere/AppServer/derby/lib/derbynet.jar;D:/IBM/WebSphere/AppServer/derby/lib/derbytools.jar;

Trouble shooting Websphere Poral 6.1 default Database Crash ( Derby database)

Recently we had issue with Websphere Portal Startup when portal crashed because of free disk space issues.

When we try to access portal URL from browser

HTTP 404 when accessing the WebSphere Portal URL from a browser , Initialization of one or more services failed

In SystemOut.log we see that  Derby database connection failed for the first time and after restart of the server we see the Couple of exceptions like

Embed SQL Exception , 'The conglomerate (7,777) requested does not exist' message

and like

SQL Exception , 'The conglomerate (15,424) requested does not exist' message ( When it tries to access the PROT_RES table in JCR schema and WPS_TASK scheduler table in RELEASE database)

Trouble Shooting Steps we followed

1. Stop the Portal server if it's still running.
2. Delete the two lock files, db.lck and dbex.lck, in <wp_profile>/PortalServer/derby/wpsdb.
3. Delete the "tranlog" for WebSphere_Portal, under <wp_profile>/tranlog/<cell>/<node>/WebSphere_Portal.
4. Restart the Portal server.
5. If the Portal server startup still has problems, remove all the files in <wp_profile>/PortalServer/derby/wpsdb/log and then restart the Portal server.
6. If all of the above fails and you have a backup of the "wpsdb" database, you may attempt to archive the current database in another location outside of the WebSphere directory structure and put the backup in place.
NOTE: Because of the potential database corruptions documented Its recommend periodic backup of your Derby databases

Ref:
http://www-01.ibm.com/support/docview.wss?uid=swg21381003


But the above steps didn't Solve our problem, Raised PMR but that didn't help .

We tried to check the above tables (PROT_RES and WPS_TASKSCH) manually by connecting Derby DB but we got the same error like above when we fire Select queries . Realized that both tables corrupt(crashed).

We restored to wpsDB (we had wpsDB backup and deleted the old one replaced it).
NOTE: Recommended a periodic backup of your Derby databases

Troubleshooting DataBase (DB) Performance Issues

At some point in any project implementation, need to troubleshoot the DB either to resolve the problem or performance.

Things to consider

1. Check the connection pool size
2. Fine Tune SQL statements
3. Execute  "UPDATESTATS" at DB side  to update the tablespace sizes and make updates to stats
4. Check the webserver I/O timeout , if it is very low (like 10sec) sometime you may receive the 500 (server timeout). HTTP server timeout should be aggregate of  considering DB response time + application response time
5. Considering the option of creating materialized view or MQT(materialized query table).