简体   繁体   中英

What is the proper way to create a datasource in a spring web application?

I have two ways to create datasource:

  1. Inside spring context

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" /> <property name="username" value="root" /> <property name="password" value="password" /> 

  2. Tomcat JNDI

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/UsersDB"/>

What are the benefits and problems of creating the datasource using spring or using tomcat jndi ?

The first approach, using the class DriverManagerDataSource, is useful for test or standalone environments outside of j2ee container.

The second approach, using the jndi data soure, is recommended to be used in the j2ee container.

The below reference clearly states these approaches.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/datasource/DriverManagerDataSource.html

There is nothing like good practice among this. The good one is the one which is good for use case. They both have pros and cons.

When u have use cases where u are trying to deploy the apps on a server environment where multiple apps share a common datasource or datasource components are managed by the Server administration then probably the best way to use is through a JNDI context. The server has the DataSource kept ready so that the application can directly get that object, bind to itself and use the datasource.

When u have use case where application runs inside a embedded server container or cloud containers where the server also is embedded along the build packs creating datasource with in the application and using them would be a good solution. Also in non web application environments creating datasource would be good.

To summarize, use cases where datasource is non managed by the application better to go for a JNDI context.

Try to figure out what is the deployment model of your application and figure out what is good for u. Also try to use a datasourcepool implementations to improve performance like DBCP, C3P0 or Hikarcp.

I am using it as below. Works for me since project is configured at the time of architecture design. And its live in production.

<!-- Local Apache Commons DBCP DataSource that refers to a combined database -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <!-- The placeholders are resolved from jdbc.properties through -->
    <!-- the PropertyPlaceholderConfigurer in applicationContext.xml -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="validationQuery" value="${jdbc.validationquery}"/>
        <property name="testOnBorrow" value="${jdbc.testonborrow}"/>
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>


    <!-- Transaction manager for a single JDBC DataSource -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

you can also refer to this. Best contribution of Genious to java developers.

https://www.mkyong.com/spring-boot/spring-boot-jdbc-oracle-database-commons-dbcp2-example/

The best practice is to not to expose the database configuration in any of the code that you have written. It is always best to configure inside the server console and expose it as a jndi. Inside spring configuration file, get the database instance as:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc"/>

and configure the database transaction manager as:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

The key note here is do not expose the database configuration.

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