0

Please help. The below is my XML configuration file. My application runs properly. But it says no database selected when call database.

The exception looks like below:

2018-03-24 13:44:17 DEBUG SqlExceptionHelper:139 - could not extract ResultSet [n/a]
java.sql.SQLException: No database selected
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

 <mvc:annotation-driven />
 <context:component-scan base-package="app.sphi" />

 <mvc:resources mapping="/res/**" location="/WEB-INF/res/" />
 
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
 </bean>
 

 <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  
  <property name="poolName" value="springHikariCP" />
  <property name="connectionTestQuery" value="SELECT 1" />
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useUnicode=true" />
  <property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
  <property name="username" value="root" />
  <property name="password" value="" />
    
 </bean>

 <!-- Create default configuration for Hibernate -->
 <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  
 </bean>

 <!-- Configure the entity manager factory bean -->
 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
  
  <!-- Set JPA properties -->
  <property name="jpaProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>    
    <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
   </props>
  </property>
  <!-- Set base package of your entities -->
  <property name="packagesToScan" value="app.sphi.model" />
  <!-- Set share cache mode -->
  <property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
  <!-- Set validation mode -->
  <property name="validationMode" value="NONE" />
 </bean>

 <!-- Configure the transaction manager bean -->
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <!-- Enable annotation driven transaction management -->
 <tx:annotation-driven transaction-manager="transactionManager" />
 <!-- Configure Spring Data JPA and set the base package of the repository interfaces -->
 <jpa:repositories base-package="app.sphi.repo"
  transaction-manager-ref="transactionManager"
  entity-manager-factory-ref="entityManagerFactory" />
 
</beans>

I will be pleased for any suggestion.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49

1 Answers1

1

Do not configure both jdbcUrl and dataSourceClassName, pick one style or the other. In this case, I suggest you stick with jdbcUrl.

brettw
  • 10,664
  • 2
  • 42
  • 59
  • Thanks a lot. I have remove dataSourceClassName and add driverClassName. Now it giving. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException – Mashukur Rahman Mar 24 '18 at 12:58