Accès JDBC avec un manager: perte de connexion

shaiulud Messages postés 404 Date d'inscription mardi 18 décembre 2001 Statut Membre Dernière intervention 15 juillet 2014 - 1 mai 2004 à 14:14
shaiulud Messages postés 404 Date d'inscription mardi 18 décembre 2001 Statut Membre Dernière intervention 15 juillet 2014 - 2 mai 2004 à 11:23
Bonjour,

j'ai implémenté un manager pour encapsuler mes accès à la base de données. Mon problème est que l'object Connection devient "null" dès que je quitte la méthode qui l'instancie. alors que le même code en vrac fonctionne. Quelqu'un à il déjà été confonté au problème et a une solution.

voici ma classe correspondant au manager

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseAccess {
private Connection conn;
private PreparedStatement prepstmnt;
private DataSource datasrc;
private String errormsg;
private Statement stmnt;

/**
* Method databaseAccess
*
* Class Constructor
*/
public DatabaseAccess() {
conn = null;
prepstmnt = null;
datasrc=null;
stmnt=null;
errormsg=null;
}

/**
* Method dbConnect
*
*
*/
public void dbConnect(String JNDIparameter) throws NamingException, SQLException {
//
// get Database Access from JNDI
try {
errormsg="";
Context initCtx = new InitialContext();
DataSource datasrc = (DataSource)initCtx.lookup("java:comp/env/jdbc/"+JNDIparameter);
Connection conn = datasrc.getConnection();
initCtx.close(); //destruction du contexte JNDI
}
catch (NamingException ne) {
errormsg=" Could not access to JNDI
\n"+ne.toString();
throw new NamingException(errormsg);
}
catch (SQLException sqle) {
errormsg ="Could not connect to Database
\n"+sqle.toString();
throw new SQLException(errormsg);
}
catch (Exception e) {
errormsg= " Could not connect to Database
\n"+e.toString();
throw new SQLException(errormsg);
}
}

/**
* Method closeResultSet
*
*
*
*/
public void closeResultSet(ResultSet rs) throws SQLException {
try {
errormsg ="";
rs.close();
}
catch (SQLException sqle){
errormsg= " Could not close recordset
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}

/**
* Method getResultSet
*
*
* @return
*
*/
public ResultSet getResultSet(String query) throws SQLException{
ResultSet rs =null;
try {
errormsg= "";
Statement stmnt = (Statement) conn.createStatement();
rs=stmnt.executeQuery(query);
}
catch (SQLException sqle){
errormsg=" Could not execute query: "+query+"
\n"+sqle.toString();
throw new SQLException(errormsg);
}
return rs;
}
/**
* Method dbClose
*
*
*/
public void dbClose() throws SQLException {
try{
errormsg ="";
if (stmnt!= null) {
stmnt.close();
}
if (conn!=null) {
conn.close();
}
}
catch(SQLException sqle) {
errormsg=" Could not close Database Access
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}

/**
* Method doRollback
*
*
*/
public void doRollback() throws SQLException {
try {
errormsg ="";
conn.rollback();
}
catch (SQLException sqle){
errormsg= " Could not rollback transaction
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}

/**
* Method doCommit
*
*
*/
public void doCommit() throws SQLException {
try {
errormsg ="";
conn.commit();
}
catch (SQLException sqle){
errormsg= " Could not commit transaction
\n"+sqle.toString();
throw new SQLException(errormsg);
}

}

/**
* Method doActionSqlQuery
*
*
*/
public void doActionSqlQuery(String query) throws SQLException {
try {
doActionSqlQuery(query, true);
}
catch (SQLException sqle){
errormsg =sqle.toString();
throw new SQLException(errormsg);
}
}

/**
* Method doActionSqlQuery
*
*
*/
public void doActionSqlQuery(String query, boolean autocommit) throws SQLException {

try {
errormsg="";
if (conn!=null) {
conn.setAutoCommit(autocommit);
} else {
System.out.println("conn perdu");
}
Statement stmnt = (Statement) conn.createStatement();
if (stmnt==null) {
System.out.println("stmnt perdu");
}
stmnt.executeQuery(query);
}
catch (SQLException sqle){
errormsg="Could not execute query: "+query+"
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
}

2 réponses

kirua12 Messages postés 1155 Date d'inscription samedi 17 janvier 2004 Statut Membre Dernière intervention 29 avril 2011 7
1 mai 2004 à 19:36
Salut,

c'est normal !!! :)
Dans ta méthode dbConnect tu déclares une varibale locale conn et tu n'utilises pas celle définie en attribut de la classe.

au lieu de
Connection conn = datasrc.getConnection();


il faut faire
conn = datasrc.getConnection();
0
shaiulud Messages postés 404 Date d'inscription mardi 18 décembre 2001 Statut Membre Dernière intervention 15 juillet 2014 22
2 mai 2004 à 11:23
Merci, j'avais fais la grosse boulette de débutant.
0
Rejoignez-nous