driverconfig.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 824 行 · 第 1/2 页
JAVA
824 行
public boolean isXATransaction() { return _xaDataSource != null && _dbPool.isXA(); } /** * Returns true if the driver is XA enabled. */ public boolean isLocalTransaction() { return _dbPool.isXA(); } /** * Configure a ProfilerPointConfig, used to create a ProfilerPoint * that is then passed to setProfiler(). * The returned ProfilerPointConfig has a default name set to the URL of * this driver, */ public ProfilerPointConfig createProfilerPoint() { ProfilerPointConfig profilerPointConfig = new ProfilerPointConfig(); profilerPointConfig.setName(getURL()); profilerPointConfig.setCategorizing(true); return profilerPointConfig; } /** * Enables profiling for this driver. */ public void setProfilerPoint(ProfilerPoint profilerPoint) { _profilerPoint = profilerPoint; } /** * Initialize the pool's data source * * <ul> * <li>If data-source is set, look it up in JNDI. * <li>Else if the driver is a pooled or xa data source, use it. * <li>Else create wrappers. * </ul> */ synchronized void initDataSource(boolean isTransactional, boolean isSpy) throws SQLException { if (! _lifecycle.toActive()) return; if (_xaDataSource == null && _poolDataSource == null) { initDriver(); Object driverObject = getDriverObject(); if (driverObject == null) { throw new SQLExceptionWrapper(L.l("driver '{0}' has not been configured for pool {1}. <database> needs a <driver type='...'>.", _driverClass, getDBPool().getName())); } if (_driverType == TYPE_XA) _xaDataSource = (XADataSource) _driverObject; else if (_driverType == TYPE_POOL) _poolDataSource = (ConnectionPoolDataSource) _driverObject; else if (_driverType == TYPE_DRIVER) _driver = (Driver) _driverObject; else if (driverObject instanceof XADataSource) _xaDataSource = (XADataSource) _driverObject; else if (_driverObject instanceof ConnectionPoolDataSource) _poolDataSource = (ConnectionPoolDataSource) _driverObject; else if (_driverObject instanceof ManagedConnectionFactory) _jcaDataSource = (ManagedConnectionFactory) _driverObject; else if (_driverObject instanceof Driver) _driver = (Driver) _driverObject; else throw new SQLExceptionWrapper(L.l("driver '{0}' has not been configured for pool {1}. <database> needs a <driver type='...'>.", _driverClass, getDBPool().getName())); /* if (! isTransactional && _xaDataSource != null) { throw new SQLExceptionWrapper(L.l("XADataSource '{0}' must be configured as transactional. Either configure it with <xa>true</xa> or use the database's ConnectionPoolDataSource driver or the old java.sql.Driver driver.", _xaDataSource)); } */ } _admin.register(); if (_profilerPoint != null) { if (log.isLoggable(Level.FINE)) log.fine(_profilerPoint.toString()); if (_xaDataSource != null) _xaDataSource = new XADataSourceWrapper(_profilerPoint, _xaDataSource); else if (_poolDataSource != null) _poolDataSource = new ConnectionPoolDataSourceWrapper(_profilerPoint, _poolDataSource); else if (_driver != null) _driver = new DriverWrapper(_profilerPoint, _driver); } if (_info.size() != 0) { validateInitParam(); } } Lifecycle getLifecycle() { return _lifecycle; } boolean start() { return _lifecycle.toActive(); } boolean stop() { return _lifecycle.toStop(); } private void validateInitParam() { if (_jcaDataSource != null) { throw new ConfigException(L.l("<init-param> cannot be used with a JCA data source. Use the init-param key as a tag, like <key>value</key>")); } else if (_poolDataSource != null) { throw new ConfigException(L.l("<init-param> cannot be used with a ConnectionPoolDataSource. Use the init-param key as a tag, like <key>value</key>")); } else if (_xaDataSource != null) { throw new ConfigException(L.l("<init-param> cannot be used with an XADataSource. Use the init-param key as a tag, like <key>value</key>")); } } /** * Returns the driver object configured for the database. */ synchronized Object getDriverObject() throws SQLException { if (_driverObject != null) return _driverObject; else if (_driverClass == null) return null; if (log.isLoggable(Level.CONFIG)) log.config("loading driver: " + _driverClass.getName()); try { _driverObject = _driverClass.newInstance(); } catch (Exception e) { throw new SQLExceptionWrapper(e); } return _driverObject; } /** * Creates a connection. */ PooledConnection createPooledConnection(String user, String password) throws SQLException { PooledConnection conn = null; if (_xaDataSource != null) { if (user == null && password == null) conn = _xaDataSource.getXAConnection(); else conn = _xaDataSource.getXAConnection(user, password); /* if (! _isTransactional) { throw new SQLExceptionWrapper(L.l("XADataSource '{0}' must be configured as transactional. Either configure it with <xa>true</xa> or use the database's ConnectionPoolDataSource driver or the old java.sql.Driver driver.", _xaDataSource)); } */ } else if (_poolDataSource != null) { /* if (_isTransactional) { throw new SQLExceptionWrapper(L.l("ConnectionPoolDataSource '{0}' can not be configured as transactional. Either use the database's XADataSource driver or the old java.sql.Driver driver.", _poolDataSource)); } */ if (user == null && password == null) conn = _poolDataSource.getPooledConnection(); else conn = _poolDataSource.getPooledConnection(user, password); } return conn; } /** * Creates a connection. */ Connection createDriverConnection(String user, String password) throws SQLException { if (! _lifecycle.isActive()) return null; if (_xaDataSource != null || _poolDataSource != null) throw new IllegalStateException(); if (_driver == null) throw new IllegalStateException(); Driver driver = _driver; String url = getURL(); if (url == null) throw new SQLException(L.l("can't create connection with null url")); try { Properties properties = new Properties(); properties.putAll(getInfo()); if (user != null) properties.put("user", user); else properties.put("user", ""); if (password != null) properties.put("password", password); else properties.put("password", ""); Connection conn; if (driver != null) conn = driver.connect(url, properties); else conn = java.sql.DriverManager.getConnection(url, properties); synchronized (this) { _connectionCountTotal++; } return conn; } catch (SQLException e) { synchronized (this) { _connectionFailCountTotal++; _lastFailTime = Alarm.getCurrentTime(); } throw e; } } @PostConstruct public void init() { if (_driverClass == null && _poolDataSource == null && _xaDataSource == null) { if (_driverURL == null) throw new ConfigException(L.l("<driver> requires a 'type' or 'url'")); String driver = DatabaseManager.findDriverByUrl(_driverURL); if (driver == null) throw new ConfigException(L.l("url='{0}' does not have a known driver. The driver class must be specified by a 'type' parameter.", _driverURL)); Class driverClass = null; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); driverClass = Class.forName(driver, false, loader); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw ConfigException.create(e); } setType(driverClass); } } /** * Initializes the JDBC driver. */ public void initDriver() throws SQLException { if (! _lifecycle.toInit()) return; Object driverObject = getDriverObject(); if (driverObject != null) { } else if (_xaDataSource != null || _poolDataSource != null) return; else { throw new SQLExceptionWrapper(L.l("driver '{0}' has not been configured for pool {1}. <database> needs either a <data-source> or a <type>.", _driverClass, getDBPool().getName())); } try { // server/14g1 if (_driverURL != null) { PropertyValueProgram program; program = new PropertyValueProgram("url", _driverURL); program.configure(driverObject); } } catch (Exception e) { if (driverObject instanceof Driver) log.log(Level.FINEST, e.toString(), e); else throw new SQLExceptionWrapper(e); } try { if (_user != null) { // && ! (driverObject instanceof Driver)) { PropertyValueProgram program; program = new PropertyValueProgram("user", _user); program.configure(driverObject); } } catch (Exception e) { log.log(Level.FINEST, e.toString(), e); if (! (driverObject instanceof Driver)) throw new SQLExceptionWrapper(e); } try { if (_password != null) { // && ! (driverObject instanceof Driver)) { PropertyValueProgram program; program = new PropertyValueProgram("password", _password); program.configure(driverObject); } } catch (Exception e) { log.log(Level.FINEST, e.toString(), e); if (! (driverObject instanceof Driver)) throw new SQLExceptionWrapper(e); } try { if (_init != null) { _init.configure(driverObject); _init = null; } Config.init(driverObject); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); throw new SQLExceptionWrapper(e); } } private boolean hasSetter(Class cl, String name) { if (true) return true; for (Method method : cl.getMethods()) { String methodName = method.getName(); if (! methodName.startsWith("set")) continue; else if (method.getParameterTypes().length != 1) continue; methodName = methodName.substring(3).toLowerCase(); if (methodName.equals(name)) return true; } return false; } // // statistics // /** * Returns the total number of connections made. */ public long getConnectionCountTotal() { return _connectionCountTotal; } /** * Returns the total number of failing connections */ public long getConnectionFailCountTotal() { return _connectionFailCountTotal; } /** * Returns the time of the last connection */ public long getLastFailTime() { return _lastFailTime; } /** * Returns a string description of the pool. */ public String toString() { return "JdbcDriver[" + _driverURL + "]"; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?