简体   繁体   中英

Does bean creation in spring datasource xml open connection to the database?

In Spring while creating beans in the following way in datasource xml does it also establishes a connection with the database? If it opens a db connection,then how it get closed?

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin@localhost:1521/myoracledb" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

Does rs.close();,ps.close(); and conn.close(); in finally block guarantees connection close where rs is ResultSet object,ps is PreparedStatement object and conn is Connection object.

I am using normal JDBC(with Spring-Datasource.xml) and springMVC and facing some issue with connection remaining open with the database.

Is there any other way to ensure that database connection remain closed in the process?

First thing

The datasource configuration in the spring-context.xml provides Simple implementation of the standard JDBC DataSource interface , configuring a plain old JDBC Driver via bean properties , and returning a new Connection for every getConnection call.

Each time you do a datasourceObject.getConnection() it will establish a new connection for you.

Secondly

Use of ( rs.close() , ps.close() and) conn.close() in a finally block guarantees connection close.

Yes it does, that is why it is recommended to close the connection in the finally because even in case of some Exception the finally block will get execute and release the resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM