📄 embeddeddatasource.java
字号:
if (dataSourceName != null) { if (!(dataSourceName.equals(ds.dataSourceName))) match = false; } else if (ds.dataSourceName != null) match = false; if (description != null) { if (!(description.equals(ds.description))) match = false; } else if (ds.description != null) match = false; if (createDatabase != null) { if (!(createDatabase.equals(ds.createDatabase))) match = false; } else if (ds.createDatabase != null) match = false; if (shutdownDatabase != null) { if (!(shutdownDatabase.equals(ds.shutdownDatabase))) match = false; } else if (ds.shutdownDatabase != null) match = false; if (connectionAttributes != null) { if (!(connectionAttributes.equals(ds.connectionAttributes))) match = false; } else if (ds.connectionAttributes != null) match = false; if (loginTimeout != ds.loginTimeout) match = false; return match; } return false; } /* * Properties to be seen by Bean - access thru reflection. */ /** Set this property to create a new database. If this property is not set, the database (identified by databaseName) is assumed to be already existing. @param create if set to the string "create", this data source will try to create a new database of databaseName, or boot the database if one by that name already exists. */ public final void setCreateDatabase(String create) { if (create != null && create.toLowerCase(java.util.Locale.ENGLISH).equals("create")) createDatabase = create; else createDatabase = null; } /** @return "create" if create is set, or null if not */ public final String getCreateDatabase() { return createDatabase; } /** Set this property if one wishes to shutdown the database identified by databaseName. @param shutdown if set to the string "shutdown", this data source will shutdown the database if it is running. */ public final void setShutdownDatabase(String shutdown) { if (shutdown != null && shutdown.equalsIgnoreCase("shutdown")) shutdownDatabase = shutdown; else shutdownDatabase = null; } /** @return "shutdown" if shutdown is set, or null if not */ public final String getShutdownDatabase() { return shutdownDatabase; } /** Set this property to pass in more Derby specific connection URL attributes. @param prop set to the list of Derby connection attributes separated by semi-colons. E.g., to specify an encryption bootPassword of "x8hhk2adf", and set upgrade to true, do the following: <PRE> ds.setConnectionAttributes("bootPassword=x8hhk2adf;upgrade=true"); </PRE> See the Derby documentation for complete list. */ public final void setConnectionAttributes(String prop) { connectionAttributes = prop; update(); } /** @return Derby specific connection URL attributes */ public final String getConnectionAttributes() { return connectionAttributes; } /** Set attributeAsPassword property to enable passing connection request attributes in the password argument of getConnection. If the property is set to true then the password argument of the DataSource.getConnection(String user, String password) method call is taken to be a list of connection attributes with the same format as the connectionAttributes property. @param attributesAsPassword true to encode password argument as a set of connection attributes in a connection request. */ public final void setAttributesAsPassword(boolean attributesAsPassword) { this.attributesAsPassword = attributesAsPassword; update(); } /** Return the value of the attributesAsPassword property. */ public final boolean getAttributesAsPassword() { return attributesAsPassword; } /* * DataSource methods */ /** * Attempt to establish a database connection. * * @return a Connection to the database * @exception SQLException if a database-access error occurs. */ public final Connection getConnection() throws SQLException { return this.getConnection(getUser(), getPassword(), false); } /** * Attempt to establish a database connection with the given username and password. If the attributeAsPassword property is set to true then the password argument is taken to be a list of connection attributes with the same format as the connectionAttributes property. * * @param username the database user on whose behalf the Connection is * being made * @param password the user's password * @return a Connection to the database * @exception SQLException if a database-access error occurs. */ public final Connection getConnection(String username, String password) throws SQLException { return this.getConnection(username, password, true); } /** @param requestPassword true if the password came from the getConnection() call. */ final Connection getConnection(String username, String password, boolean requestPassword) throws SQLException { Properties info = new Properties(); if (username != null) info.put(Attribute.USERNAME_ATTR, username); if (!requestPassword || !attributesAsPassword) { if (password != null) info.put(Attribute.PASSWORD_ATTR, password); } if (createDatabase != null) info.put(Attribute.CREATE_ATTR, "true"); if (shutdownDatabase != null) info.put(Attribute.SHUTDOWN_ATTR, "true"); String url = jdbcurl; if (attributesAsPassword && requestPassword && password != null) { StringBuffer sb = new StringBuffer(url.length() + password.length() + 1); sb.append(url); sb.append(';'); sb.append(password); // these are now request attributes on the URL url = sb.toString(); } Connection conn = findDriver().connect(url, info); // JDBC driver's getConnection method returns null if // the driver does not handle the request's URL. if (conn == null) throw Util.generateCsSQLException(SQLState.PROPERTY_INVALID_VALUE,Attribute.DBNAME_ATTR,getDatabaseName()); return conn; } InternalDriver findDriver() throws SQLException { String url = jdbcurl; if (driver == null || !driver.acceptsURL(url)) { synchronized(this) { // The driver has either never been booted, or it has been // shutdown by a 'jdbc:derby:;shutdown=true' if (driver == null || !driver.acceptsURL(url)) { new org.apache.derby.jdbc.EmbeddedDriver(); // If we know the driver, we loaded it. Otherwise only // work if DriverManager has already loaded it. driver = (InternalDriver) DriverManager.getDriver(url); // DriverManager will throw an exception if it cannot find the driver } } } return driver; // else driver != null and driver can accept url } void update() { StringBuffer sb = new StringBuffer(64); sb.append(Attribute.PROTOCOL); // Set the database name from the databaseName property String dbName = getDatabaseName(); if (dbName != null) { dbName = dbName.trim(); } if (dbName == null || dbName.length() == 0) { // need to put something in so that we do not allow the // database name to be set from the request or from the // connection attributes. // this space will selected as the database name (and trimmed to an empty string) // See the getDatabaseName() code in InternalDriver. Since this is a non-null // value, it will be selected over any databaseName connection attribute. dbName = " "; } sb.append(dbName); String connAttrs = getConnectionAttributes(); if (connAttrs != null) { connAttrs = connAttrs.trim(); if (connAttrs.length() != 0) { sb.append(';'); sb.append(connectionAttributes); } } jdbcurl = sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -