Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jdbc question 1

Status
Not open for further replies.

tvrtko

Programmer
Nov 26, 2003
53
HR
I use connection pooling with Tomcat. I configured connection poll in Tomcat server configuration file
and access it via JNDI. Everything works fine, just compiler gives warning: "ZadaciAction.java uses unchecked or unsafe operations".
This is the piece of code where that happens:


try
{
InitialContext ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDB");

conn = ds.getConnection(); // Connection object

String query = "SELECT * FROM zadaci where godina = ? and rok = ?";

stmt = conn.prepareStatement(query);

stmt.setString(1, (String)rundata.getSession().getAttribute(Defines.GODINA));

rs = stmt.executeQuery();

while (rs.next()){

Zadaci zadatak = new Zadaci(rs.getString(3), rs.getString(4), cTemp); // my object

zadaci.add(nTemp, zadatak); // ArrayList object

nTemp = nTemp + 1; // integer counter
}

}
catch (Exception e)
{
rundata.setMessage("Greska: " + e.toString());
rundata.setStackTrace(org.apache.turbine.util.StringUtils.stackTrace(e), e);

} finally {

if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (rs1 != null) {
try { rs1.close(); } catch (SQLException e) { ; }
rs1 = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (stmt1 != null) {
try { stmt1.close(); } catch (SQLException e) { ; }
stmt1 = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}

What is the reason?

Is it necessery to sinchronize getting connection
"conn = ds.getConnection();"
or sinchronization is implemented in getConnection() method?

Can I put DataSource object in the context and share it among users?
Are there any synchronization issues about that?


Thanks in advance.
 
Thanks for your help.
It solved my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top