Connection to Database Hibernate, JDBC through proxy server

Viewed 353

I have a desktop app(.exe), which is using a Database in cloud, in hibernate config file I have set up a url\port\user\password of server, on which database is setup. I tested this out at home and is working fine.

But, at my job, where I want to use this app, there are some problems. In our office we have set this up by our system administrators proxy server, so all connections goes through it.

And from this point - my connection to DB in app is not working, because application does not now anything about proxy server, I need to implement it in my code.

I have ip/port/user/pass for connection to proxy, but I have no understanding how to setup this proxy in my program, I tried to search about this in internet, but there is no result, which could help.

a searched and founded this article by oracle https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html but there is no setting to setup user and password for proxy server authentication, so it do not help (although I tried this).

here is by hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://linkToServer</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
        <property name="hibernate.show_sql">true</property>

    </session-factory>
</hibernate-configuration>

Here is my HibernateSessionFactoryUtil.java:

package database;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class HibernateSessionFactoryUtil {
    private static SessionFactory sessionFactory;

    private HibernateSessionFactoryUtil(){}

    public static SessionFactory getSessionFactory(){
        if(sessionFactory == null) {
            try {

                Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

                configuration.addAnnotatedClass(User.class);
                configuration.addAnnotatedClass(Worker.class);
                configuration.addAnnotatedClass(Task.class);
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                                .applySettings(configuration.getProperties());

                sessionFactory = configuration.buildSessionFactory(builder.build());

            } catch (Exception e){
                System.out.println("Exception:" + e);
            }
        }
        return sessionFactory;
    }
}

little example of using DB in code:

 public void save(User user) {
        Session session = HibernateSessionFactoryUtil.getSessionFactory().openSession();
        Transaction transaction1 = session.beginTransaction();
        session.save(user);
        transaction1.commit();
        session.close();
    }
0 Answers
Related