0

I'm new to MyBatis and am having an issue with a test class failing to connect to the database because the property placeholders aren't being resolved. As soon as I replace the ${placeholder} settings with a hard coded strings, it works fine.

I've searched quite a bit and found threads like this one context:property-placeholder doesn't resolve references . Unfortunately adding the sqlSessionFactoryBeanName property to the MapperScannerConfigurer had no effect. No matter what I've tried, the test class ONLY works when I hard code the connection settings.

Could anyone help me spot what I'm doing wrong .. while I still have some hair left? If you need more information, just ask.

Relevant versions:

  • springframework 4.3
  • mybatis version 3.5.0
  • mybatis-spring version 1.3.2
  • dbcp version 2.6

Here's my latest attempt (sorry for the messy code)

myproperties.properties

driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin@//host:port/someservicename
username=someusername
password=somepassword

applicationContext.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlacholderConfigurer">
  <property name="locations">
    <list>
      <value>classpath:myproperties.properties</value>
    </list>
  </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${driverClassName}"/>
  <property name="url" value="${url}"/>
  <property name="username" value="${username}"/>
  <property name="password" value="${password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="typeAliasesPackage" value="com.abc.model"/>
  <property name="mapperLocations" value="classpath*:MyMapper.xml"/>
</bean>

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

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
    p:basePackage="com.abc.mappers"
    p:sqlSessionFactoryBeanName="sqlSessionFactory"
    p:processPropertyPlaceholders="true" />

mybatis-config.xml

<configuration>
   <typeAliases>
      <typeAlias ..../>
   </typeAliases>

   <environments default="dev">
      <environment id="dev">
         <transactionManager type="JDBC"/>  
         <dataSource type="POOLED">
             <property name="driver" value="${driverClassName}"/>
             <property name="url" value="${url}"/>
             <property name="username" value="${username}"/>
             <property name="password" value="${password}"/>
          </dataSource>        
      </environment>
   </environments>

   <mappers>
      <mapper resource="/path/to/MyMapper.xml"/>
   </mappers>

</configuration>

Test class fails to connect with an error like this

### Error querying database. Cause org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (IO Error: The Netword Adapter could not establish the connection)

thebluephantom
  • 16,458
  • 8
  • 40
  • 83
masi
  • 1
  • 1

1 Answers1

0

Never fails. Spend hours on a problem and you find the answer as soon as you post a question.

Turns out the reason the placeholders weren't resolving is because the property names were accidentally duplicated, way down at the bottom of myproperties.properties:

driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin@//host:port/someservicename
username=someusername
password=somepassword
...
...
#duplicated property names
driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin@//host:port/someservicename
username=someusername
password=somepassword

As soon as I removed the duplicates, the placeholders resolved. Hopefully this silly mistake will help someone else.

masi
  • 1
  • 1