I'm getting "While trying to lookup 'jdbc.LogDB' didn't find subcontext 'jdbc'. Resolved ''" error when i try to get connection from the Weblogic. I created and tested the datasource and it's working. Also created the target servers. Datasource name is "jdbc/LogDb". Below is the test code i wrote.
public class TestUtils {
private static final Logger logger = LoggerFactory.getLogger(TestUtils.class.getName());
public DataSource ds = null;
public String testConnectionPool() throws SQLException {
Context ctx = null;
Hashtable ht = new Hashtable();
Connection conn;
String result = "";
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ht.put(Context.SECURITY_PRINCIPAL, "weblogic");
ht.put(Context.SECURITY_CREDENTIALS, "weblogic");
try {
ctx = new InitialContext(ht);
ds = (DataSource) ctx.lookup("jdbc/LogDB");
logger.debug("Weblogic Connection Pool Created");
conn = ds.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("some sql");
ResultSet rs = stmt.getResultSet();
if(rs.next()){
result = result + rs.getString(1);
}
stmt.close();
conn.close();
} catch (NamingException e) {
logger.error("Naming Exception occured at connect: " + e.getMessage());
} catch (Exception e){
logger.error("Exception occured at connect: "+ e.getMessage());
} finally {
try {
if (ctx != null) {
ctx.close();
}
}
catch (Exception e) {
logger.error("Ctx Error");
}
}
return result;
}
}
I tried the following names
"java:jdbc/LogDb"
"java:comp/env" variations etc.
Thanks for the kind answers
Related
I was working on a java project and it was working just fine. I was able to make connections. I closed all the connections properly in finally block. Now I am not able to make connections or even open psql in my terminal. How can I make it work as before. Much much appreciated
import java.sql.Connection;
import com.mchange.v2.c3p0.*;
public class MyConnection {
public static Connection getConnection(){
ComboPooledDataSource cpds1 = new ComboPooledDataSource();
String dbDriver = "org.postgresql.Driver";
String dbName = "jdbc:postgresql://localhost/postgres";
cpds1.setJdbcUrl(dbName);
String userName = "user_1";
cpds1.setUser(userName);
String password = "mypass";
cpds1.setPassword(password);
cpds1.setMaxStatements( 180 );
try
{
cpds1.setDriverClass(dbDriver);
return cpds1.getConnection();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
This is where I'm calling it
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter writer = response.getWriter();
JSONObject jo = new JSONObject();
JSONObject jObj;
Statement stmt = null;
Connection con = null;
PreparedStatement ps;
ResultSet rs = null;
try
{
jObj = UtilityClass.getJSON(request);
String uname = ((String) jObj.get("uname"));
String pass = ((String) jObj.get("pass"));
String sql = "SELECT * FROM users WHERE username = ?";
try
{
con = MyConnection.getConnection();
System.out.println("Got Connection");
stmt = con.createStatement();
ps = con.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
if(rs.next())
{
if(BCrypt.checkpw(pass,rs.getString("password")))
{
HttpSession session = request.getSession();
session.setAttribute("uname", uname);
if(session.isNew())
{
System.out.println("new");
}
if(uname.equals("admin"))
{
session.setAttribute("role", "admin");
jo.put("status", "admin");
}
else
{
session.setAttribute("role", "user");
jo.put("status", "authenticate");
}
}
}
writer.print(jo);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Not Connected");
}
finally
{
if(rs != null)
{
rs.close();
}
if(stmt != null)
{
stmt.close();
}
if(con != null)
{
con.close();
}
}
}
catch(Exception e)
{
System.out.print("JSON Exception");
}
}
Usually, DB Admins are using pooling technologies on Databases. For PostgreSQL one of the more popularly is a PGBOUNCER. We used PGBOUNCER in our large project, the result is excellent. I recommend it to you. To get more information about the pooling system you can read this link. For About Pooling
Why connection close doesnt work in this code? I tried way without finally block by creating connection in try() but after executing testDbConnection a few times I have too many connections error and when I am trying to connect to my database by pgAdmin I see too many clients connected. How can I resolve it? Why closing connection doesn't work?
private List<DataSource> getDataSources() {
connectionsNumber = 2;
List<DataSource> dataSources = new ArrayList<>();
for (int i = 1; i <= connectionsNumber; i++) {
Connection connection;
DataSource dataSource;
String jdbcUrl = environment.getProperty(String.format("database%d.url", i));
String user = environment.getProperty(String.format("database%d.username", i));
String password = environment.getProperty(String.format("database%d.password", i));
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass("org.postgresql.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
cpds.setJdbcUrl(jdbcUrl);
cpds.setUser(user);
cpds.setPassword(password);
cpds.setMinPoolSize(3);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxIdleTime(1);
cpds.setMaxConnectionAge(600);
cpds.setMaxStatements(500);
dataSource = cpds;
dataSources.add(dataSource);
}
return dataSources;
}
public void testDbConnection() throws SQLException {
String query = "select id from users;";
Statement st = null;
ResultSet rs = null;
Connection connection = null;
List<DataSource> dataSources = getDataSources();
for (DataSource dataSource : dataSources) {
try {
connection = dataSource.getConnection();
st = connection.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
System.out.println("Connection");
}
} finally {
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
if (connection != null) {
connection.close();
}
st = null;
rs = null;
connection = null;
}
}
}
In my opinion,when using the c3p0, connection.close() is not really closing the connection, just put it back in the pool.if you want to clean up the DataSource,you can use DataSources.destroy(dataSource);
I have a working connection with mysql database:
public static javax.sql.DataSource getJDBCDataSource() throws DAOException
{
try {
if (_ds == null)
{
_ds = (javax.sql.DataSource) getContext().lookup(
CommonUtilities.getServerProperties().getProperty( PropertyFileConfigurationKeys.CONFIG_SERVER_MYSQL_JNDI.toString()
)
);
}
return _ds;
} catch (NamingException ex) {
throw new DAOException("Problem looking up resource by name; see chained exception.", ex);
}
}
The JNDI for the mySQL database is defined in persistence.xml.
It is giving a nullpointer exception when am trying to run a query for testing:
try{
DataSource ds = BeanUtilities.getJDBCDataSource();
InterviewerProperty ip1 = new InterviewerProperty(ds);
Collection<EmployeeVO> results = ip1.getEmployees();
for(EmployeeVO next:results) {
System.out.println(next);
}
}
catch(DAOException e){
System.out.println("Exception occured");
System.out.println(e);
}
}
The method getEmployees accessing the database is:
public ArrayList<EmployeeVO> getEmployees() throws DAOException {
Connection connection = null;
PreparedStatement statement = null;
ArrayList<EmployeeVO> result = new ArrayList<EmployeeVO>();
try {
connection = getConnection();
statement = connection.prepareStatement("select * from employee");
ResultSet rs = statement.executeQuery();
while(rs.next()) {
result.add((EmployeeVO) rs);}
rs.close();
} catch (SQLException ex) {
String err = "Unable to fetch Employees";
throw new DAOException(err,ex);
} finally {
close(statement,connection);
}
return result;
}
The error is:
**Exception in thread "main" java.lang.NullPointerException
at javax.naming.InitialContext.getURLScheme(InitialContext.java:269)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:318)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at edu.ufl.bebr.scheduler.framework.BeanUtilities.getJDBCDataSource(BeanUtilities.java:57)
at edu.ufl.bebr.scheduler.ejb3.session.TestRun.main(TestRun.java:44)**
Any help regarding how to fix the error would be great.
Thanks in advance!
I wrote a servlet and run it on tomcat6. When there is a query sent to the specified DNS, the servlet will get information and use them to search in the database(mysql), then write some thing back. I use connection pool in the tomcat6. However, when I'm trying to do a load test on my program, I found the throughput is extremely low and many exceptions like
Response code: Non HTTP response code:
java.net.SocketTimeoutException Non HTTP response message: Read timed
out
Your result:
Team and Account info\n at java.net.SocketInputStream.socketRead0(Native Method); at
java.net.SocketInputStream.read(SocketInputStream.java:152); at
java.net.SocketInputStream.read(SocketInputStream.java:122); at
org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166); at
org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90); at
org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281); at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92); at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:61); at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254); at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289); at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252); at
org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191); at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300); at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127); at
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715); at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520); at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906); at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805); at
org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:475); at
org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:295); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1105); at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1094); at
org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:429); at
org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257); at
java.lang.Thread.run(Thread.java:745)
Can somebody give me some suggestions? Here is my code of servlet and this is the only Java code I use in this web project, thanks
#SuppressWarnings("serial")
public class Servlet extends HttpServlet {
#Override
public void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
Connection conn = null;
DataSource ds = null;
InitialContext ctx;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
final Statement stmt = conn.createStatement();
stmt.executeQuery("SET NAMES 'utf8mb4'; ");
} catch (final SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
res.setContentType("text/plain;charset=UTF-8");
final PrintWriter pw = res.getWriter();
try {
final Statement stmt = conn.createStatement();
String teamInfo = "aa\n";
ResultSet rs = null;
final String info = req.getQueryString();
final String[] sp = info.split("&");
final String[] child0 = sp[0].split("=");
final String[] child1 = sp[1].split("=");
final String sqlString = "select * from tweets where timeandid=\"" + child1[1] + child0[1] + "\"";
rs = stmt.executeQuery(sqlString);
rs.next();
final String a = rs.getString(2);
teamInfo = teamInfo + rs.getString(3) + ":" + rs.getString(4).trim() + ":" + a + "\n";
pw.print(teamInfo);
pw.flush();
rs.close();
stmt.close();
} catch (final SQLException ex) {
System.out.println(ex);
} finally {
System.out.println("over");
}
pw.close();
try {
conn.close();
} catch (final SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
It's been a while since I have done any Java programming. And I find my self a bit stuck.
My problem is that I have a pooled db connection in tomcat. That is working nicely. But there is a lot of boiler plate required.
public void init() {
Connection conn = null;
ResultSet rst = null;
Statement stmt = null;
try {
//SETUP
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env/jdbc");
OracleDataSource ds = (OracleDataSource) envContext.lookup("tclsms");
if (envContext == null) throw new Exception("Error: No Context");
if (ds == null) throw new Exception("Error: No DataSource");
if (ds != null) conn = ds.getConnection();
if (conn == null) throw new Exception("Error: No Connection")
message = "Got Connection " + conn.toString() + ", ";
//BODY
stmt = conn.createStatement();
rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");
if (rst.next()) message = rst.getString(1);
//TEAR DOWN
rst.close();
rst = null;
stmt.close();
stmt = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we don't close it twice
} catch (Exception e) {
e.printStackTrace();
//TODO proper error handling
} finally {
// Always make sure result sets and statements are closed,
// and the connection is returned to the pool
if (rst != null) {
try {
rst.close();
} catch (SQLException e) {;}
rst = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {;}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {;}
conn = null;
}
} //END FINALLY
} //END INIT
So I want to do the equivalent of passing a method into init that will run in the body of the function. I know I can't do this in Java. But I'm sure there must be a nice way to do this. Or at least a best practice for this sort of thing.
Any help much appreciated.
abstract class UseDBConnectionTask extends Runnable {
private Connection conn;
public UseDBConnectionTask(){
setUp();
}
// should probably refine this to specific exceptions
public abstract void process() throws Exception;
public void run(){
try{
process()
// this should catch specific exceptions
} catch (Exception e){
// handle
} finally {
tearDown();
}
}
Connection getConnection(){
return conn;
}
public void setUp(){
// SETUP here
// set the conn field
}
public void tearDown(){
// TEAR DOWN here
}
}
use like:
UseDBConnectionTask dbTransaction = new UseDBConnectionTask(){
public void process(){
// do processing
// use conn via getConnection()
// eg
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");
String message = null;
if (rst.next()) message = rst.getString(1);
}
}
new Thread(dbTransaction).start();
The advantage of extending Runnable is that you can then pass this instance into a thread pool or similar.
Just have to be careful of threading issues. It also assumes that the tear down is always the same.
You should prefer delegation to inheritance. The above can/will work but isn't well thought out.
Implementing Runnable on the primary class exposes it for abuse because the 'run()' method is public.
A second improvement is to use to delegate your activity to an interface (and this CAN be passed around like a function pointer whereas extending the class cannot). In addition, it makes it Spring friendly
This allows the action implementer to decide if they want multi-threaded behavior or not. You can inject composites, caching delegates, etc and the primary class is none-the-wiser. This conforms with good design practice of separation of concerns
public class MyClass {
private Action action;
public MyClass (Action action) {
this.action = action;
}
public void connection() {
try{
action.perform()
} catch (Exception e){
// handle
} finally {
tearDown();
}
}
Connection getConnection(){
return conn;
}
private void setUp(){
// SETUP here
// set the conn field
}
private void tearDown(){
// TEAR DOWN here
}
}
interface IDbAction {
public DbActionResult runAction(Connection conn);
}
class DbActionResult {
Statement statement;
ResultSet resultSet;
public DbActionResult(Statement statement, ResultSet resultSet){
this.statement = statement;
this.resultSet = resultSet;
}
public void getStatement(){ return this.statement; }
public void getResultSet(){ return this.resultSet; }
}
public void runAgainstDB(IDbAction action) {
Connection conn = null;
ResultSet rst = null;
Statement stmt = null;
try {
//SETUP
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env/jdbc");
OracleDataSource ds = (OracleDataSource) envContext.lookup("tclsms");
if (envContext == null) throw new Exception("Error: No Context");
if (ds == null) throw new Exception("Error: No DataSource");
if (ds != null) conn = ds.getConnection();
if (conn == null) throw new Exception("Error: No Connection")
message = "Got Connection " + conn.toString() + ", ";
//BODY
DbActionResult actionResult = action.runAction(conn);
//TEAR DOWN
if((rst = actionResult.getResultSet()) != null){
rst.close();
rst = null;
}
if((stmt = actionResult.getStatement()) != null){
stmt.close();
stmt = null;
}
actionResult = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we don't close it twice
} catch (Exception e) {
e.printStackTrace();
//TODO proper error handling
} finally {
// Always make sure result sets and statements are closed,
// and the connection is returned to the pool
if (rst != null) {
try {
rst.close();
} catch (SQLException e) {;}
rst = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {;}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {;}
conn = null;
}
} //END FINALLY
} //END
Use like:
IDbAction action = new IDbAction(){
public DbActionResult prcoessAction(Connection conn){
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");
if (rst.next()) message = rst.getString(1);
return new DbActionResult(stmt, rst);
}
}
runAgainstDB(action);
private void Todo(Context initContext, Context envContext, OracleDataSource ds){
if (envContext == null) throw new Exception("Error: No Context");
if (ds == null) throw new Exception("Error: No DataSource");
if (ds != null) conn = ds.getConnection();
if (conn == null) throw new Exception("Error: No Connection")
message = "Got Connection " + conn.toString() + ", ";
//BODY
stmt = conn.createStatement();
rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");
if (rst.next()) message = rst.getString(1);
//TEAR DOWN
rst.close();
rst = null;
stmt.close();
stmt = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we don't close it twice
} catch (Exception e) {
e.printStackTrace();
//TODO proper error handling
} finally {
// Always make sure result sets and statements are closed,
// and the connection is returned to the pool
if (rst != null) {
try {
rst.close();
} catch (SQLException e) {;}
rst = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {;}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {;}
conn = null;
}
} //END FINALLY
}
Then call it from your Init like this this. Todo(initContext,envContext , ds)