Hi, I wanted to encrypt the key I have to the database in the file hibernate.cfg.xml, and I found Jasypt can do it, I followed these tutorials.
I used the encrypt.bat and put the parameters to return the key to the encrypted database, the configuration of my hibernate.cfg.xml file stayed this way.
<property name="connection.provider_class">org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider</property>
<property name="connection.encryptor_registered_name">configurationHibernateEncryptor</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/aprendiendojsf?autoReconnect=true</property>
<property name="connection.username">root</property>
<property name="connection.password">ENC(lhFCMXdRkAw7Cz5JU17lvg==)</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
My HibernateUtil class in this way
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setAlgorithm("PBEWithMD5AndDES");
encryptor.setPassword("clave");
HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance();
registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
My project gets up correctly, but at the time of login, this appears in the log:
INFO: HHH000046: Connection properties: {user=root, encryptor_registered_name=configurationHibernateEncryptor, password=****}
WARN: HHH000342: Could not obtain connection to query metadata : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
I do not understand what may be happening, I already have 3 days in this and I can not give, I hope you can help me, it would be a great help!
I made the following change in the Hibernate Util class, which was suggested to me.
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
try {
EnvironmentStringPBEConfig envConfig = new EnvironmentStringPBEConfig();
envConfig.setPasswordEnvName(“DB_SIMETRIC_KEY”);
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setAlgorithm("PBEWithMD5AndDES");
encryptor.setConfig(envConfig);
HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance();
registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
However the apache console launches the same thing to me
WARN: HHH000342: Could not obtain connection to query metadata : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
I thought it was easier to configure.