473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with Database while using JNDI Lookup.

dmjpro
2,476 2GB
I thought that when the connection gets closed then all resources get closed .
I also included the ResultSet and Statement ;) . But if the Statement closed then only ResultSet closed. But now i got the point ..after closing Connection i am still able to access to iterate the ResultSet . Actually it is tested only with Postgres. Is it sun specified : ?

Now i tested another code ...
Expand|Select|Wrap|Line Numbers
  1. import org.apache.tomcat.dbcp.dbcp.cpdsadapter.DriverAdapterCPDS;
  2. import org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource;
  3.  
  4.         DriverAdapterCPDS l_adapter = new DriverAdapterCPDS();
  5.         l_adapter.setDriver("org.postgresql.Driver");
  6.         l_adapter.setUrl("jdbc:postgresql://10.29.32.68:5432/Admin");
  7.         l_adapter.setUser("erp");
  8.         l_adapter.setPassword("iiterp");
  9.  
  10.         SharedPoolDataSource l_ds = new SharedPoolDataSource();
  11.         l_ds.setConnectionPoolDataSource(l_adapter);
  12.         Connection l_con = l_ds.getConnection();
  13.  
  14.         Statement l_stmt = l_con.createStatement();
  15.         ResultSet l_rs = l_stmt.executeQuery("select * from dpcode");
  16.         l_con.close();
  17.         while(l_rs.next()){System.out.println("Yahoo!!!");}
  18.  
The Resultset is still getting iterated ...

But when I get the DataSource using the JNDI look up then my another code is not working ..Saying that the ResultSet is closed.
Could you help me to figure out ...
Expand|Select|Wrap|Line Numbers
  1.             Context l_ctx = new InitialContext();
  2.             DataSource l_ds =                     (DataSource)l_ctx.lookup("java:/comp/env/jdbc/admindatasource");
  3.             Connection l_con = l_ds.getConnection();
  4.  
  5.             Statement l_stmt = l_con.createStatement();
  6.             ResultSet l_rs = l_stmt.executeQuery("select * from dpcode");
  7.             l_con.close();
  8.             while(l_rs.next()){System.out.println("Yahoo!!!");}
  9.  
Please help me to figure out .. ;)
Jan 21 '09 #1
9 2637
JosAH
11,448 Expert 8TB
A Connection is the owner of a Statement. A Statement is the owner of a ResultSet. When you close an object earlier in the chain the other objects, later in the chain, are also closed. A PooledConnection isn't closed when you get rid of it, i.e. it is simply returned to its pool. Read the API documentation for the close() method of a Connection: "If the close method is called and there is an active transaction, the results are implementation-defined. "

kind regards,

Jos
Jan 21 '09 #2
dmjpro
2,476 2GB
Then why does it happen to me ???
U mean that whats are happening that should not be ?
I could not get you ..yeah i know .when a connection(obtained from pool) closed then it simply goes to Pool and be reused later.
But these things are happening to me ?
Jan 21 '09 #3
JosAH
11,448 Expert 8TB
What part of "implementation-defined" was so difficult to understand? You're closing an object earlier in the chain: a Connection; nothing can be said about a Statement or ResultSet. The Connection could just be dumped in a pool again or it may be really closed.

kind regards,

Jos
Jan 21 '09 #4
dmjpro
2,476 2GB
Yeah all these things are implementation dependent ... But still there should be a standard logic to implement ....
Jan 21 '09 #5
r035198x
13,262 8TB
@dmjpro
Yes there is. Close the rs first, then the statement and then the connection.
Jan 21 '09 #6
dmjpro
2,476 2GB
Then why does it behave like a hell ???
Yeah i know ... i should close those resources sequentially ... But the code already developed .. it requires a lot of hurdle ...
That's why i was asking .. if make a raw connection and even also using a DataSource it working fine .. but if i get the DataBase using JNDI look up then it's not working .. i was looking for this answer ?
Jan 21 '09 #7
JosAH
11,448 Expert 8TB
@dmjpro
Because the code assumed a particular implementation defined behaviour; that's an error; e.g. in C and C++ you have to stay far away from "undefined behaviour" and "implementation defined behaviour". Same here: there's an error in the code; fix it and don't ask why anymore.

kind regards,

Jos
Jan 21 '09 #8
dmjpro
2,476 2GB
Don't get me wrong .......... According to the answer of r0... i asked why does this ... anyway the designed was wrong ;)
Jan 21 '09 #9
JosAH
11,448 Expert 8TB
@dmjpro
Erm, yes, that's what I was trying to tell you all the time in replies #2, #4 and #8, but glad you understand it now.

kind regards,

Jos
Jan 21 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
0
by: Arun | last post by:
Hi I am using the following Tomcat: 4.1.27-LE-jdk14 Apache: 2.0.47 Java : 1.4.2 Linux OS: 7.3 I am trying to configure LDAP authentication mechanism to my
1
by: Bridget Willey | last post by:
I am using ACT 6 and am trying to "split" the database between records for customers and junk records. The accounts designated as "customers" have that word in the ID field, and I am using that...
2
by: David | last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll get an answer, so trying here. I'm trying to convert a script to use friendly URLs, I've done this before, but my PHP...
1
by: sajithamol | last post by:
What’s the difference between JNDI lookup(), list(), listBindings(), and search()?
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
10
dmjpro
by: dmjpro | last post by:
My context.xml... <Context path="/JNDITest"> <Resource name="jdbc/dataSource" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"...
0
dmjpro
by: dmjpro | last post by:
When i do .... HashMap props = new HashMap(); props.put(Context.INITIAL_CONTEXT_FACTORY,"FactoryClass"); Context ctx = new InitialContext(props); Here it is obvious that the proper...
1
by: webster5u | last post by:
Hi, who's has experience to open connection pool in Tomcat 5.5 using JNDI? I recently research on it, i found the how-to guideline provided from apache tomcat website. It seem like not really work...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.