1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
@Stateful(mappedName = "ejb.daoreportingINOA")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DaoReportingINOA implements IDaoReportingINOARemote, IDaoReportingINOALocal {
private Connection con = null;
private String url = "";
private String user = "";
private String pass = "";
@Override
public void init(String url, String user, String pass) {
if (con==null || !this.url.equals(url) || !this.user.equals(user) || !this.pass.equals(pass)) {
System.out.println("EJB INOA Statefull: nouvelle base demandée, je me connecte");
this.url = url;
this.user = user;
this.pass = pass;
dbDeconnecte();
dbConnecte();
}
else
System.out.println("EJB INOA Statefull: même base demandée, inutile que je me reconnecte");
}
private void dbConnecte() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("demande de connection vers " + url + "...");
con = DriverManager.getConnection("jdbc:mysql://" + url, user, pass);
System.out.println("connection vers " + url + " Ètablie");
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
} catch (SQLException e) {
System.out.println("Probleme SQL dans dbConnecte " + e.toString());
} catch (Exception e) {
System.out.println("Exception : " + e.toString());
}
}
@PreDestroy
private void dbDeconnecte() {
try {
System.out.println("Deconnection de " + url + "");
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.out.println("Probleme dbDeconnecte : " + e.toString());
}
}
} |
Partager