⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xaconnectionfactory.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setPSCacheSize(int size) {
        psCacheSize = size;
    }

    /**
     * Gets the number of PreparedStatements to be cached for each
     * Connection.  The default value is 10.
     */
    public int getPSCacheSize() {
        return psCacheSize;
    }


    /**
     * Gets the transaction isolation level of connections.  This defaults to
     * whatever the connection's default iso level is.
     */
    public int getTransactionIsolation() {
        return transactionIsolation;
    }

    public void setTransactionIsolation(int iso) {
        this.transactionIsolation = iso;
    }

    public void setTransactionIsolation(String iso) {
        if (iso.equals("TRANSACTION_NONE")) {
            this.transactionIsolation = Connection.TRANSACTION_NONE;
        } else if (iso.equals("TRANSACTION_READ_COMMITTED")) {
            this.transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
        } else if (iso.equals("TRANSACTION_READ_UNCOMMITTED")) {
            this.transactionIsolation = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else if (iso.equals("TRANSACTION_REPEATABLE_READ")) {
            this.transactionIsolation = Connection.TRANSACTION_REPEATABLE_READ;
        } else if (iso.equals("TRANSACTION_SERIALIZABLE")) {
            this.transactionIsolation = Connection.TRANSACTION_SERIALIZABLE;
        } else {
            throw new IllegalArgumentException("Setting Isolation level to unknown state: " + iso);
        }
    }

    /**
     * Sets the XADataSource used to generate XAConnections.  This may be
     * supplied by the vendor, or it may use the wrappers for non-compliant
     * drivers (see XADataSourceImpl).
     * @see org.ofbiz.minerva.pool.jdbc.xa.wrapper.XADataSourceImpl
     */
    public void setDataSource(XADataSource dataSource) {
        source = dataSource;
    }

    /**
     * Gets the XADataSource used to generate XAConnections.
     */
    public XADataSource getDataSource() {
        return source;
    }

    /**
     * Sets the TransactionManager.
     */
    public void setTransactionManager(TransactionManager tm) {
        this.tm = tm;
    }

    /**
     * Gets the TransactionManager.
     */
    public TransactionManager getTransactionManager() {
        return this.tm;
    }


    /**
     * Have XAClientConnections save a stack trace on creation
     * This is useful for debugging non-closed connections.
     * It must be used with ReleaseOnCommit option
     */
    public boolean getSaveStackTrace() {
        return saveStackTrace;
    }

    public void setSaveStackTrace(boolean save) {
        saveStackTrace = save;
    }

    /**
     * Verifies that the data source and transaction manager are accessible.
     */
    public void poolStarted(ObjectPool pool) {
        if (log.isDebugEnabled())
            log.debug("Starting");

        super.poolStarted(pool);
        this.pool = pool;
        if (source == null)
            throw new IllegalStateException("Must specify XADataSource to " + getClass().getName());
        if (source instanceof XADataSourceImpl) {
            ((XADataSourceImpl) source).setSaveStackTrace(saveStackTrace);
        }

        /*
        if(tmJndiName == null)
            throw new IllegalStateException("Must specify TransactionManager JNDI Name to "+getClass().getName());
        if(ctx == null)
            throw new IllegalStateException("Must specify InitialContext to "+getClass().getName());
        try {
            tm = (TransactionManager)ctx.lookup(tmJndiName);
        } catch(NamingException e) {
            throw new IllegalStateException("Cannot lookup TransactionManager using specified context and name!");
        }
        */
    }

    /**
     * Creates a new XAConnection from the provided XADataSource.
     */
    public Object createObject(Object parameters) throws Exception {

        log.debug("Opening new XAConnection");

        Object obj = null;
        try {
            if (parameters != null) {
                String credentials[] = (String[]) parameters;
                if (credentials.length == 2)
                    obj = source.getXAConnection(credentials[0], credentials[1]);
            } else if (userName != null && userName.length() > 0)
                obj = source.getXAConnection(userName, password);
            else
                obj = source.getXAConnection();
        } catch (SQLException e) {
            log.error("Can't get an XAConnection", e);
            throw e;
        }
        return obj;
    }

    /**
     * Registers the XAConnection's XAResource with the current transaction (if
     * there is one).  Sets listeners that will handle deregistering and
     * returning the XAConnection to the pool via callbacks.
     */
    public Object prepareObject(Object pooledObject) {
        boolean trace = log.isDebugEnabled();
        XAConnection con = (XAConnection) pooledObject;
        con.addConnectionEventListener(listener);
        Transaction trans = null;
        try {
            if (tm.getStatus() != Status.STATUS_NO_TRANSACTION) {
                trans = tm.getTransaction();
                XAResource res = con.getXAResource();
                rms.put(con, res);
                trans.enlistResource(res);
                if (trace)
                    log.debug("Resource '" + res + "' enlisted for '" + con + "'.");
            } else {
                if (trace)
                    log.debug("No transaction right now.");
            }
        } catch (Exception e) {
            //System.out.println("error in prepareObject!!!!!");
            e.printStackTrace();
            log.error("Unable to register with TransactionManager", e);
            con.removeConnectionEventListener(listener);
            throw new RuntimeException("Unable to register with TransactionManager: " + e);
        }

        if (con instanceof XAConnectionImpl) {
            ((XAConnectionImpl) con).setTransactionListener(transListener);
            ((XAConnectionImpl) con).setPSCacheSize(psCacheSize);
            if (transactionIsolation != DEFAULT_ISOLATION) {
                try {
                    ((XAConnectionImpl) con).setTransactionIsolation(transactionIsolation);
                } catch (SQLException sex) {
                    throw new RuntimeException("Unable to setTransactionIsolation: " + sex.getMessage());
                }
            }

            if (trans != null) {
                //System.out.println("inserting con: " + con + "into wrapperTx, tx: " + trans);
                wrapperTx.put(con, trans); // For JDBC 1/2 wrappers, remember which
                wrapperTx.put(trans, con); // connection goes with a given transaction
            }
        }
        return con;
    }

    /**
     * Closes a connection.
     */
    public void deleteObject(Object pooledObject) {
        XAConnection con = (XAConnection) pooledObject;
        try {
            con.close();
        } catch (SQLException e) {
        }
    }

    /**
     * If a new object is requested and it is a JDBC 1/2 wrapper connection
     * in the same Transaction as an existing connection, return that same
     * connection.
     */
    public Object isUniqueRequest() {
        try {
            if (tm.getStatus() != Status.STATUS_NO_TRANSACTION) {
                Transaction trans = tm.getTransaction();
                //System.out.println("isUniqueRequest returning conn: " + wrapperTx.get(trans) + "  attached to tx: " + trans);
                return wrapperTx.get(trans);
            }
        } catch (Exception e) {
        }
        return null;
    }

    /** For XAConnectionImpl check that parameters = String[2]{username, password}
     and that these match the the source connection user and password. Return
     true for non-XAConnectionImpl sources
     */
    public boolean checkValidObject(Object source, Object parameters) {
        boolean validObject = true;
        if (parameters != null && source instanceof XAConnectionImpl) {
            XAConnectionImpl con = (XAConnectionImpl) source;
            String credentials[] = (String[]) parameters;
            if (credentials.length == 2) {
                String user = con.getUser();
                String password = con.getPassword();
                boolean validUser = ((user == null) && (credentials[0] == null)) || ((user != null) && user.equals(credentials[0]));
                boolean validPassword = ((password == null) && (credentials[1] == null)) || ((password != null) && password.equals(credentials[1]));
                validObject = validUser && validPassword;
            }
        }
        return validObject;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -