This following way data source binding with the InitialContext is used for when you are implementing Junit testing in the project because when you run Junit testing the project is not deploy so if you initialized data source in tomee.xml and web.xml is not initialize.so for this reason you have to invoked one method for the initialize the data source problematically like following way.
Here i am showing example with java and MySQL if you are using any other database then you just need to
change 'url' properties and 'driverClassName' properties.
Here i created two methods
1)initialdsn()
This method binding the data source with InitialContext so in future we can get using jndi lookup.
2)checkdsn()
This method is showing that how you will access data source.
NOTE:Don't forget to include jar file into the classpath.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DSNDEMO {
public static void initialdsn()throws Exception
{
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES,"org.apache.naming");
Properties properties = new Properties ();
properties.setProperty("url", "jdbc:mysql://localhost:3306/ops");
properties.setProperty("driverClassName","com.mysql.jdbc.Driver");
properties.setProperty("maxActive", "10");
properties.setProperty("maxIdle", "8");
properties.setProperty("minIdle", "10");
properties.setProperty("maxWait", "10");
properties.setProperty("testOnBorrow", "true");
properties.setProperty("username", "root");
properties.setProperty("password", "");
properties.setProperty("validationQuery", "SELECT 1");
properties.setProperty("removeAbandoned", "true");
properties.setProperty("removeAbandonedTimeout", "1");
properties.setProperty("logAbandoned", "true");
DataSource ds = BasicDataSourceFactory.createDataSource(properties);
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");
ic.bind("java:/comp/env/jdbc/mydb", ds);
}
public static void checkdsn()throws Exception
{
Context initContext = new InitialContext();
Context webContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) webContext.lookup("jdbc/mydb");
Connection con=ds.getConnection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select *from student_master");
while(rs.next())
{
System.out.println(rs.getString(1));
}
}
public static void main(String args[])throws Exception
{
DSNDEMO.initialdsn();
DSNDEMO.checkdsn();
}
}
For this you have to download this naming-common-4.1.31.jar and you need to include in your classpath or you can download from here
So for the above program you need to include following jar file into classpath.
1)naming-common-4.1.31.jar (This contains class org.apache.naming.java.javaURLContextFactory)
2)commons-pool-1.5.7.jar
3)commons-dbcp-1.4.jar
4)mysql-connector-java-3.0.17-ga-bin
for Junit you create one method and include the initialdsn() source code in that method.
if you are getting any exception then free for comment here.