Java 获取错误指定的 Oracle URL 无效:OracleDataSource.makeURL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19577569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting error Invalid Oracle URL specified: OracleDataSource.makeURL
提问by Basit
I am getting error when I try to run my application on Server. I am suing eclipse and glass fish server 4. I made a glassfish-resources.xml file and put it in the WEB-INF directory. When I try to run on server. I get the following exception
当我尝试在服务器上运行我的应用程序时出现错误。我起诉eclipse和glassfish server 4。我做了一个glassfish-resources.xml文件,放到WEB-INF目录下。当我尝试在服务器上运行时。我收到以下异常
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause:
Connection could not be allocated because: Invalid Oracle URL specified:
OracleDataSource.makeURL
Error Code: 0
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:316)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:135)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:330)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:307)
.....
Here is my glassfish-resources.xml file
这是我的 glassfish-resources.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1
Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool name="java:app/myOracleConnectionPool"
res-type="javax.sql.ConnectionPoolDataSource"
datasource-classname="oracle.jdbc.pool.OracleConnectionPoolDataSource">
<property name="User" value="system" />
<property name="Port" value="1521" />
<property name="DatabaseName" value="XE" />
<property name="ServerName" value="127.0.0.1" />
<property name="Url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="Password" value="xxxx" />
</jdbc-connection-pool>
<jdbc-resource enabled="true"
jndi-name="java:app/jdbc/myOracleDatasource"
object-type="user"
pool-name="java:app/myOracleConnectionPool">
<description />
</jdbc-resource>
</resources>
here is my persistence.xml file
这是我的 persistence.xml 文件
<persistence-unit name="chapter11PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:app/jdbc/myOracleDatasource</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="Oracle"/>
<property name="javax.persistence.schema-generation-action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation-target" value="scripts"/>
<property name="javax.persistence.ddl-create-script-target" value="createfoo.sql"/>
<property name="javax.persistence.ddl-drop-script-target" value="dropfoo.sql"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.application-location" value="/tmp"/>
<!-- To log SQL queries -->
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
I have also put ojdbc6.jar in the lib/ext folder. I am using oracle 11g release 2. Why I am getting invalid url error?
我还将 ojdbc6.jar 放在 lib/ext 文件夹中。我使用的是 oracle 11g 版本 2。为什么我收到无效的 url 错误?
I have also check the connection in the SQL Developer and it is working, with user system and my password.
我还检查了 SQL Developer 中的连接,它正在使用用户系统和我的密码工作。
Thanks
谢谢
回答by Shailendra
Looks like you have mistakenly given the url two times and the property name is not correct.
看起来您错误地提供了两次 url 并且属性名称不正确。
<property name="Url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
It should be
它应该是
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
Also it would be a good idea to refer to the documentation of glassfish 4.1 to get the correct property names you can use in glassfish-resources.xml
此外,参考 glassfish 4.1 的文档以获取您可以在 glassfish-resources.xml 中使用的正确属性名称也是一个好主意
For Glassfish 3.1 the options are given here
对于 Glassfish 3.1,这里给出了选项
回答by Basit
I have solved the issue. Actually I am using EJB 3.1, JSF 2.2, Java EE 7 and glass fish 4. My JSF controller call EJB which perform CRUD operations. I have something like this in persistence.xml
我已经解决了这个问题。实际上我正在使用 EJB 3.1、JSF 2.2、Java EE 7 和 glassfish 4。我的 JSF 控制器调用执行 CRUD 操作的 EJB。我在persistence.xml中有这样的东西
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="chapter11PU" transaction-type="JTA">
<jta-data-source>java:app/jdbc/myOracleDatasource</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="Oracle"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
<property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL.sql"/>
<property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL.sql"/>
<property name="eclipselink.application-location" value="E:/eclipse-kepler/workspace/Java EE 7/ch11_ProcessAndNavigation/src/main/resources/" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<!-- To log SQL queries -->
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
Note:
笔记:
The "java:app/jdbc/myOracleDatasource" data source required by the persistence unit
must be created within the EJB container. And there are several ways to do so. The
simplest one is to have a @DataSourceDefinition annotation on any Managed Bean. The
container will deploy the bean and create the data source. Another way is to use the
GlassFish interface.
So I used something like this on my EJB
所以我在我的 EJB 上使用了这样的东西
@Named
@Stateless
@DataSourceDefinition(
className = "oracle.jdbc.pool.OracleConnectionPoolDataSource",
name = "java:app/jdbc/myOracleDatasource",
user = "system",
password = "xxxx",
databaseName = "XE",
portNumber=1521,
properties={"url=jdbc:oracle:thin:@localhost:1521:XE"}
)
public class BookEJB {
....
}
What I was missing is the properties={"url=jdbc:oracle:thin:@localhost:1521:XE"}
attribute. So it was giving me error. After adding this attribute to @DataSourceDefinition
. The error gone and things start working fine.
我缺少的是properties={"url=jdbc:oracle:thin:@localhost:1521:XE"}
属性。所以它给了我错误。将此属性添加到@DataSourceDefinition
. 错误消失了,一切开始正常。
Thank you
谢谢