Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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 ?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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

about 4 years ago · Santiago Trujillo Denunciar

0

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.

about 4 years ago · Santiago Trujillo Denunciar

0

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/

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda