Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package cl.ipciisa.desarrolloComponentes.business.dao.impl;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. import javax.naming.InitialContext;
  9. import javax.naming.NamingException;
  10. import javax.sql.DataSource;
  11.  
  12. import cl.ipciisa.desarrolloComponentes.resources.Resources;
  13.  
  14. public class DataSourceFactory {
  15.    
  16.     private static final String BD_DRIVER = "com.mysql.jdbc.Driver";
  17.     private static final String BD = "prueba";
  18.     private static final String BD_URL = "jdbc:mysql://localhost/" + BD;
  19.     private static final String BD_USER = "root";
  20.     private static final String BD_PASSWD = "root";
  21.    
  22.  
  23.     /**
  24.      * esta clase es la que decide a donde se conecta Si esta corriendo tomcat obtiene la conexion desde el contexto si no,
  25.      * obtiene la conexion directamente
  26.      *
  27.      * @return
  28.      * @throws DAOException
  29.      */
  30.     public static Connection getConnection() throws DAOException {
  31.  
  32.         Connection con = null;
  33.  
  34.         try {
  35.             con = getConnectionFromContext();
  36.         } catch (DAOException e) {
  37.             con = getConnectionDirectly();
  38.         } catch (SQLException e) {
  39.             throw new DAOException(e.getMessage());
  40.         }
  41.  
  42.         return con;
  43.     }
  44.  
  45.     private static Connection getConnectionFromContext() throws DAOException, SQLException {
  46.  
  47.         InitialContext cxt = null;
  48.         DataSource ds = null;
  49.  
  50.         try {
  51.  
  52.             cxt = new InitialContext();
  53.             if (cxt == null)
  54.                 throw new DAOException("No hay contexto...!");
  55.  
  56.             ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/DB_mySQL_Prueba");
  57.  
  58.             if (ds == null)
  59.                 throw new DAOException("No se ha encontrado conexión en el contexto!");
  60.  
  61.             Connection con = ds.getConnection();
  62.             con.setAutoCommit(false);
  63.  
  64.             return con;
  65.  
  66.         } catch (NamingException e) {
  67.             throw new DAOException(e.getMessage());
  68.         }
  69.  
  70.     }
  71.  
  72.     private static Connection getConnectionDirectly() throws DAOException {
  73.  
  74.         Connection con = null;
  75.         try {
  76.             Class.forName(BD_DRIVER);
  77.         } catch (ClassNotFoundException e) {
  78.             System.out.println("Error : " + e.getMessage());
  79.         }
  80.         try {
  81.             con = DriverManager.getConnection(BD_URL, BD_USER, BD_PASSWD);
  82.  
  83.             con.setAutoCommit(false);
  84.             try {
  85.                 PreparedStatement pst = con.prepareStatement("SELECT 1 FROM DUAL");
  86.                 pst.executeQuery();
  87.                 pst.close();
  88.             } catch (Exception e) {
  89.                 con.close();
  90.             }
  91.         } catch (SQLException sqle) {
  92.             throw new DAOException("DataSourceFactory.getConnectionDirectly() failed!");
  93.         }
  94.         return con;
  95.     }
  96.  
  97.  
  98.     /**
  99.      * Cierra la conecccion a la base de datos recibida como argumento solo si es distinta de la conexion estatica.
  100.      *
  101.      * @param con
  102.      *            Coneccion a cerrar
  103.      *
  104.      */
  105.     public static void desconectar(Connection con) {
  106.  
  107.         if (con != null)
  108.             try {
  109.                 if (!con.isClosed())
  110.                     con.close();
  111.             } catch (SQLException sqle) {
  112.                 throw new RuntimeException("DataSourceFactory.desconetar() failed!");
  113.             }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement