一般java 项目用ssh或ssm 都是通过spring配置文件连接数据源,个人在项目中一般采用
<bean id="test" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:test1"/> 此处是通过sid去连接oracle数据库
<property name="username" value="admin"/>
<property name="password" value="admin"/>
<property name="maxActive" value="100" />
<property name="maxIdle" value="2" />
<property name="maxWait" value="5000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="true" />
</bean>
但是在最近的项目中采用此方式连接时报以下错误
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
但是通过pl/sql连接,是能正常访问数据库的。此处是因为pl/sql连接是通过 oracle的SERVICE_NAME去连接,而不是通过sid去连接。
解决方法
1、如果有数据库的权限可以通过以下权限直接查询
select instance_name from V$instance;
查到正确的sid后就直接修改配置就行。
2、采用以下连接方式通过service_name方式连接
<bean id="test1" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=test1)))"/>
<property name="username" value="admin"/>
<property name="password" value="admin"/>
<property name="maxActive" value="100" />
<property name="maxIdle" value="2" />
<property name="maxWait" value="5000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="true" />
</bean>