hibernate org.h2.jdbc.JdbcSQLException:连接中断:“连接异常:连接被拒绝:连接:本地主机”[90067-193]

wmomyfyw  于 2022-11-30  发布在  其他
关注(0)|答案(2)|浏览(299)

任何人都可以告诉我我代码有什么问题吗

休眠配置

package com.app.EcommerceBackend.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages={"com.app.EcommerceBackend.dto"})
@EnableTransactionManagement
public class HibernateConfig {

    // Change the below based on the DBMS you choose
    private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/ecommerce";
    private final static String DATABASE_DRIVER = "org.h2.Driver";
    private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
    private final static String DATABASE_USERNAME = "sa";
    private final static String DATABASE_PASSWORD = "";

    // dataSource bean will be available
    @Bean
    public DataSource getDataSource() {

        BasicDataSource dataSource = new BasicDataSource();

        // Providing the database connection information
        dataSource.setDriverClassName(DATABASE_DRIVER);
        dataSource.setUrl(DATABASE_URL);
        dataSource.setUsername(DATABASE_USERNAME);
        dataSource.setPassword(DATABASE_PASSWORD);

        return dataSource;

    }

    // sessionFactory bean will be available

    @Bean
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);

        builder.addProperties(getHibernateProperties());
        builder.scanPackages("com.app.EcommerceBackend.dto");

        return builder.buildSessionFactory();

    }


    // All the hibernate properties will be returned in this method 
    private Properties getHibernateProperties() {

        Properties properties = new Properties();

        properties.put("hibernate.dialect", DATABASE_DIALECT);      
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");

        //properties.put("hibernate.hbm2ddl.auto", "create");

        return properties;
    }

    // transactionManager bean
    @Bean
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;
    }

}

类别测试用例

package com.app.EcommerceBackend.test;

import static org.junit.Assert.assertEquals;

import java.sql.SQLException;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.app.EcommerceBackend.dao.CategoryDAO;
import com.app.EcommerceBackend.dto.Category;

public class CategoryTestCase {

    private static AnnotationConfigApplicationContext context;

    private static CategoryDAO categoryDAO;

    private Category category;

    @BeforeClass
    public static void init() throws Exception
    {
        org.h2.tools.Server.createTcpServer().start();

        context = new AnnotationConfigApplicationContext();
        context.scan("com.app.EcommerceBackend");
        context.refresh();
        categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
    }


    @Test
    public void testCRUDcategory()
    {
        //Adding operation
        category = new Category();

        category.setName("Mobile");
        category.setDescription("This is some description for Mobile!");
        category.setImageURL("MOB01.png");

        assertEquals("Successfully added a category inside the table!", true, categoryDAO.add(category));

        category = new Category();

        category.setName("Television");
        category.setDescription("This is some description for Television!");
        category.setImageURL("TEL02.png");

        assertEquals("Successfully added a category inside the table!", true, categoryDAO.add(category));

        //Fetching and Updating  Category
        category=categoryDAO.get(2);
        category.setName("TV");
        assertEquals("Successfully updated a single category in the table!", true, categoryDAO.update(category));

        //Deleting A category
        category=categoryDAO.get(1);
        assertEquals("Successfully deleted a single category in the table!", true, categoryDAO.delete(category));

        //Fetching the List
        /*assertEquals("Successfully fetched the list of category from the table!", 1, categoryDAO.list().size());*/
    }

}

获取错误

HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException:
    Could not open Hibernate Session for transaction; nested exception is
     org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection

java.sql.SQLException:无法创建PoolableConnectionFactory(连接已断开:“连接异常:连接被拒绝:连接:本地主机”[90067-193])数据库服务器连接:本地主机“[90067 - 193])数据库服务器连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接:本地主机连接
org.h2.jdbc.JdbcSQLException:连接中断:“连接异常:连接被拒绝:如果您有任何问题,请与我们联系。如果您有问题,请与我们联系。如果您有问题,请与我们联系。

您也可以查看我的gitHub代码https://github.com/sunilgit1/ECommerce

bpzcxfmw

bpzcxfmw1#

检查您正在运行的h2数据库引擎的版本。并确保pom.xml文件中的h2版本相同。如果不相同,请将其更改为相同的版本。
如果h2数据库引擎为1.4.196,则通过pom.xml为h2数据库驱动程序添加的相关性也应为1.4.196。

ipakzgxi

ipakzgxi2#

我发现,要使H2与tcp一起工作,您需要在服务器模式下启动H2
要在服务器模式下启动服务器,您需要创建一个TCP服务器,因此下面的配置将为您提供1个应用程序的连接。

import org.h2.tools.Server;

     @Bean(initMethod = "start", destroyMethod = "stop")
        public Server h2Server1() throws SQLException {
            return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9080");
        }

如果您想让更多应用程序通过TCP连接到同一个H2服务器,您需要再添加一个示例-使用不同的端口

// Second App
    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server2() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9081");
    }

相关问题