Beispiel #1
0
  public static Connection getConnection() {
    try {

      if (datasource == null) {
        // Context context =  new InitialContext();
        // datasource = (DataSource) context.lookup("geopista");
        StandardXADataSource standardXAdataSource = new StandardXADataSource();
        standardXAdataSource.setDriverName(
            DBPropertiesStore.getDBContext().getString(DBConstants.DDBBCONNECTION_DRIVER));
        standardXAdataSource.setUrl(
            DBPropertiesStore.getDBContext().getString(DBConstants.DDBBCONNECTION_URL));
        standardXAdataSource.setUser(
            DBPropertiesStore.getDBContext().getString(DBConstants.DDBBCONNECTION_USERNAME));
        standardXAdataSource.setPassword(
            DBPropertiesStore.getDBContext().getString(DBConstants.DDBBCONNECTION_PASSWORD));
        datasource = standardXAdataSource;
      }
      Connection connection = datasource.getConnection();

      addConexion();
      // logger.debug("connection: " + connection);

      return connection;

    } catch (Exception ex) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      ex.printStackTrace(pw);
      logger.error("Exception: " + sw.toString());
      return null;
    }
  }
  public MultipleConnection(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println(USAGE);
      System.exit(1);
    }

    // first, load the properties from the spy.properties file
    Properties prop = new Properties();
    try {
      prop.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
    } catch (Exception e) {
      System.err.println("problem to load properties.");
      e.printStackTrace();
      System.exit(1);
    }

    login = prop.getProperty("login");
    password = prop.getProperty("password");
    url = prop.getProperty("url");
    driver = prop.getProperty("driver");

    // completion is either "commit", either "rollback"
    String completion = args[0];

    // get the new value which will be assign to the database
    int newValue = 0;
    try {
      newValue = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
      System.err.println(USAGE);
      System.err.println("[number] has to be an integer\n");
      System.exit(1);
    }

    // Get a transaction manager
    try {
      // creates an instance of JOTM with a local transaction factory which is not bound to a
      // registry
      jotm = new Jotm(true, false);
      InitialContext ictx = new InitialContext();
      ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
    } catch (Exception e) {
      System.err.println("JOTM problem.");
      e.printStackTrace();
      System.exit(1);
    }

    // get an user transaction
    UserTransaction utx = null;
    try {
      System.out.println("create initial context");
      Context ictx = new InitialContext();
      System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME);
      utx = (UserTransaction) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
    } catch (Exception e) {
      System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
      System.err.println("Exception message :" + e.getMessage());
      e.printStackTrace();
      System.exit(1);
    }

    // create an XA pool datasource with a minimum of 4 objects
    StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4);
    spds.setMaxSize(15);
    spds.setMinSize(13);
    spds.setUser(login);
    spds.setPassword(password);

    // create an XA datasource which will be given to the XA pool
    StandardXADataSource xads = new StandardXADataSource();
    try {
      xads.setDriverName(driver);
      xads.setUrl(url);
      xads.setUser(login);
      xads.setPassword(password);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    // give the XA datasource to the pool (to create futur objects)
    spds.setTransactionManager(jotm.getTransactionManager());
    spds.setDataSource(xads);

    try {
      System.out.println("** begin a transaction **");
      utx.begin();

      conn = spds.getConnection(login, password);
      PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY);
      pstmt.setInt(1, newValue);
      pstmt.executeUpdate();
      System.out.println("dump, after the first update:");
      printTable();
      pstmt.close();
      conn.close();

      conn = spds.getConnection(login, password);
      PreparedStatement pstmt2 = conn.prepareStatement(SQL_QUERY);
      pstmt2.setInt(1, newValue + 2);
      pstmt2.executeUpdate();
      System.out.println("dump, after the second update:");
      printTable();
      pstmt2.close();
      conn.close();

      if (completion.equals("commit")) {
        System.out.println("** commit ** the transaction");
        utx.commit();
      } else {
        System.out.println("** rollback ** the transaction");
        utx.rollback();
      }
    } catch (Exception e) {
      System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
      System.err.println("Exception message :" + e.getMessage());
      e.printStackTrace();
      System.exit(1);
    }

    System.out.println("dump, after work:");
    conn = spds.getConnection(login, password);
    printTable();
    conn.close();
    stop();
  }