0
votes

I am developing a java Spring Hibernate application with mssql database. But i am not able to establish connection with mssql database.

[Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection]

my applicationContext.xml is given below:

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

    <!-- Enable autowire -->
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.adapt.smileboard"></context:component-scan>

    <mvc:annotation-driven></mvc:annotation-driven> 

    <mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" ></property>
        <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=test;" ></property>
        <property name="username" value="user" ></property>
        <property name="password" value="pwd" ></property>
    </bean>

    <!-- Session Factory Declaration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" ></property>
        <property name="packagesToScan" value="com.adapt.smileboard.entity" ></property>
        <property name="hibernateProperties">
            <props>
                <!-- SQL Dialect -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <!-- Your required Database Name -->
                <prop key="hibernate.default_schema">smileBoardDB</prop>

                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="use_sql_comments">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>
</beans>`
1
Are you still having the issue with "hibernate.dialect" set to org.hibernate.dialect.SQLServerDialect ? At what point does it fail when you debug into the connectivity code? - Roddy of the Frozen Peas

1 Answers

1
votes

You are using a Microsoft SQL Server driver and MySQL dialect.

Use org.hibernate.dialect.SQLServerDialect for Microsoft SQL Server.

Refer this.