I am using the follwing class as my connection manager to make DB connectvity through datasource. The problem is that when I invoke this class through my DAOImpl class it is returning null.
private static DataSource dataSource;
private static Connection connection;
private ConnectionFactory() {
System.out.println(" ConnPoolFactory cons is called ");
}
public static synchronized Connection getConnection() throws SQLException {
try {
if (connection == null) {
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/myoracle");
connection = dataSource.getConnection();
} else {
return connection;
}
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println(connection);
return connection;
//System.out.println(connection);
}
- I am able to make this class work if I use
DriverManager
for conectivity. - I am able to achieve JDBC connectvity through servlet class using a
DataSource
.
But with the above code I get the following exception:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:
the following is my context.xml
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" name="jdbc/myoracle" password="password" type="javax.sql.DataSource" url="jdbc:oracle:thin:@10.49.116.42:1521:DBNAME" username="username"/>