Pages

Wednesday, August 25, 2010

JNDI Basics

Java Naming and Directory Interface (JNDI)

Naming and Directory Concepts:
Naming Concepts :
In computer system every program that we write, create files we gave them a name to identify. so giving name to a object helps to lookup into system.
so nameing service typically map object with user friendly name to its address into the system. These objects are used by a program in system.
for e.g. the Internet Domain Name System (DNS) maps machine names (such as www.sun.com) to IP addresses (such as 192.9.48.5). A file system maps a filename (for example, c:\bin\autoexec.bat) to a file handle that a program can use to access the contents of the file.
To look up an object in a naming system, you supply it the name of the object. The naming system determines the syntax that the name must follow. This syntax is sometimes called the naming system's naming convention.
For example, the UNIXTM file system's naming convention is that a file is named from its path relative to the root of the file system, with each component in the path separated from left to right using the forward slash character ("/"). The UNIX pathname, /usr/hello, for example, names a file hello in the file directory usr, which is located in the root of the file system.

Bindings:
The association of a name with an object is called a binding. For example, a file name is bound to a file.

The DNS contains bindings that map machine names to IP addresses. An LDAP name is bound to an LDAP entry. (Lightweight Directory Access Protocol)

References and Addresses:
Depending on the naming service, some objects are stored directly and some are not, objects which are stored directly are mapped to its address and those are not
are mapped as references i.e. pointer to its location. in naming service. a reference gives information about how to access an object.

Context:
A context is a set of name-to-object bindings. Every context has an associated naming convention. A context provides a lookup (resolution) operation that returns the object and may provide operations such as those for binding names, unbinding names, and listing bound names. A name in one context object can be bound to another context object (called a subcontext) that has the same naming convention.
like a DNS domain, such as COM, is a context. A DNS domain named relative to another DNS domain is a subcontext. For example, in the DNS domain Sun.COM, the DNS domain Sun is a subcontext of COM.

Naming Systems and Namespaces:
A naming system is a program that stores set of contexts of the same type (they have the same naming convention) and provides a common set of operations. \
for e.g. DNS provides naming service of mapping name to ip address, LDAP provides naming service of mapping name to Directories and files etc.
A namespace is the set of names in a naming system.



JNDI Overview
The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM programming language. It is defined to be independent of any specific directory service implementation. Thus a variety of directories--new, emerging, and already deployed--can be accessed in a common way.
The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services
The JNDI is included in the Java 2 SDK, v1.3 and later releases.
To use the JNDI, you must have the JNDI classes and one or more service providers. Java 2 SDK, v1.3 and later releases aleredy includes following
service provider classes in it

  • Lightweight Directory Access Protocol (LDAP)
  • Common Object Request Broker Architecture (CORBA) Common Object Services (COS) name service
  • Java Remote Method Invocation (RMI) Registry

Other service providers can be downloaded from the JNDI Web site or obtained from other vendors

The JNDI is divided into five packages:

* javax.naming
* javax.naming.directory
* javax.naming.event
* javax.naming.ldap
* javax.naming.spi


References:

for more details and tutorial in depth please have a look at following link

sun tutorial for JNDI


sample Naming example from sun site

This example shows you how to write a program that looks up an object whose name is passed in as a command-line argument.


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

class Lookup {
public static void main(String[] args) {
// Check that user has supplied name of file to lookup
if (args.length != 1) {
System.err.println("usage: java Lookup ");
System.exit(-1);
}

String name = args[0];

// Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");

try {

// Create the initial context
Context ctx = new InitialContext(env);

// Look up an object
Object obj = ctx.lookup(name);

// Print it out
System.out.println(name + " is bound to: " + obj);

// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.err.println("Problem looking up " + name + ": " + e);
}
}
}

Monday, December 28, 2009

Understanding GC for Memory Problems

Hi,
This is my blog post on my organisation blog site.
Please do visit part1 and part 2
Understanding GC for Memory Problems: Part-1
Understanding GC for Memory Problems: Part-2

Thursday, September 24, 2009

unable to connect to MySQL Or MySql is not Allowing more Connections

some times aborting connections or interrupting process lead to another problem to server from stop giving connections to client. that is
server do not allow any more connections to client.
probable causes would be.
client is idle more than wait_timeout or
due to long queries getting aborted from client or
client makes mistake in authentications while connecting to database.
all these lead to increase in aborted_client counter of server variable.

and if max_connect_errors= (default is 10) is set to very low will cause a server to give more connection to client when it goes beyond limit
so its better to set it something more (normally 5000-10000) which will minimize this error.

please see more ref. I found for same

http://dev.mysql.com/doc/refman/5.0/en/blocked-host.html
http://dev.mysql.com/doc/refman/5.0/en/communication-errors.html
http://lists.mysql.com/internals/33476

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

I have noticed this error in my MySql Outage, suddenly going down due to overload.
after my observation I found that this kind of error mostly will cause because of
system variables settings of wait_timeout and interactive_timeout on mysql server is set lower than datasource
idle timeout period.
my datasource pooling property for conection time out is set to 1 min
on server variable wait_timeout is set to 28 sec. (28800 milisec)

in most cases where connection is idle from host and if server wait_timeout is less than client datasource timeout
server closes connection after its sleep time is more than wait_timeout or interactive_timeout period. and client is unaware of this
and when he fires query again on same connection gets above error.

to avoid this there are following ways
1. if you are using plain connection using jconnector set autoconnect property to true.
2. if you have custom connection poolilng machanism try to validate your connection before using it from pool.
3. in other case try to set your server variable values for wait_timeout, interactive_timeout greater than your datasource timeout.

Monday, August 31, 2009

Upgrading MySQL 4.x to 5.x

This note give brief about how to uninstall MYSQL 4.1.xx and install MYSQL 5.1.xx on Linux OS
lately I have found similar steps on following site you can take help of this as well
http://confluence.atlassian.com/display/DISC/Prerequisites+and+installation+on+Fedora+or+RHEL+Linux

first steps to find where current version of MYSQL has been installed
its location of binaries, datafiles and its config file (my.inf/cnf) if any

1. give following command to check MYSQL processes running
 
[root@RAJ ~]# ps ax | grep mysqld

from its output note down location of
mysqld running (/home/raj/mysql/bin/mysqld)
--basedir=/home/raj/mysql (location where mysql binaries are located)
--datadir=/var/lib/mysql (data files of databases created)
--defaults-extra-file =/home/raj/mysql/data/my.cnf (config file if any specifed other than /etc path)

2. now stop the server
 
[root@RAJ ~]# mysqladmin shutdown

3. now fire command in step one again to verify that no process is running.

4. now check which packages are installed of MYSQL
 
[root@RAJ ~]# rpm -qa | grep mysql
mysql-4.1.22-2.el4
mysqlclient10-3.23.58-4.RHEL4.1

5. try to uninstall with rpm command for the above displayaed packags
 
[root@RAJ ~]# rpm -e mysql-4.1.22-2.el4
error: Failed dependencies:
libmysqlclient.so.14 is needed by (installed) perl-DBD-MySQL-2.9004-3.1.centos4.i386
libmysqlclient.so.14 is needed by (installed) cyrus-sasl-sql-2.1.19-14.i386
libmysqlclient.so.14(libmysqlclient_14) is needed by (installed) cyrus-sasl-sql-2.1.19-14.i386


perl-DBD-MySQL = A bundle to install Perl drivers for MySQL 4 since for mysql5 we will not need it
cyrus-sasl-sql = SASL is the Simple Authentication and Security Layer, mysql5 will have in its own package installation

by looking at error we know that there is dependancy by other files so try to remove those files first
if you require pearl for mysql5 there are newer version of perl driver you need to install.
NOTE: I could have removed it with --nodeps parameter which i come to know later so you may try with this parameter as follows
rpm --nodeps --allmatches -e mysql-4.1.22-2.el4
rpm --nodeps --allmatches -e mysqlclient10-3.23.58-4.RHEL4.1


6. remove dependencies (pls check note above)
 
[root@RAJ ~]# rpm -e perl-DBD-MySQL-2.9004-3.1.centos4.i386
[root@RAJ ~]# rpm -e cyrus-sasl-sql-2.1.19-14.i386

7. now try again step 5 again (pls check note above)
 
[root@RAJ ~]# rpm -e mysql-4.1.22-2.el4
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave

8. remove mysqlclient (pls check note above)
 
[root@RAJ ~]# rpm -e dovecot-0.99.14-1.1.el3.rf.i386
[root@RAJ ~]#
[root@RAJ ~]# rpm -e mysqlclient10-3.23.58-4.RHEL4.1

9. after this is done pls check data and binary folder of mysql is removed or not if not removed then rename it if you require them or can safely remove it

10. I have downloaded following rpm from mysql site
 
[root@RAJ installers]# ls -l
total 25864
-rw-r--r-- 1 root root 7256227 Aug 18 06:02 MySQL-client-community-5.1.37-0.rhel4.i386.rpm
-rw-r--r-- 1 root root 19179293 Aug 18 01:50 MySQL-server-community-5.1.37-0.rhel4.i386.rpm
[root@RAJ installers]# chmod 755 *

11. Install new version of mysql 5 server and client
 
[root@RAJ installers]# rpm -i MySQL-server-community-5_1.1.37-0.rhel4.i386.rpm
warning: MySQL-server-community-5_1.1.37-0.rhel4.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5
090818 7:52:19 [Warning] Forcing shutdown of 2 plugins
090818 7:52:20 [Warning] Forcing shutdown of 2 plugins

It shows some more message on screen and automatically starts mysql server.
 
[root@RAJ installers]# rpm -i MySQL-client-community-5.1.37-0.rhel4.i386.rpm
warning: MySQL-client-community-5.1.37-0.rhel4.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5

this completes the installation of mysql server and clients
there are some post installation steps to be folllowed to configure mysql if needed
like setting up my.cnf file to /etc location or creating auto start script of mysql on start of server. etc.

12. check mysql version now
 
[root@RAJ Whitehedge]# mysql -V
mysql Ver 14.14 Distrib 5.1.37, for pc-linux-gnu (i686) using readline 5.1