-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Dynamic configuration
PostPosted: Wed Aug 27, 2008 4:16 am 
Newbie

Joined: Mon Jul 16, 2007 10:44 am
Posts: 15
Hi,

I'm trying to make the parameters of the user and the path of BD configurable from another XML file other than hibernate.cfg.xml.
To do this I'm using this code :
Code:
           Configuration configuration = new Configuration();
      configuration.configure();
      
      /* Initialiser le fichier hibernate.cfg.xml */
      
      Preferences dataBasePrefs = prefs.node("database");
      
      Properties propreties1 = new Properties();
      String str1 = dataBasePrefs.get("connection.username", "");
      propreties1.put("connection.username", str1);
      
      Properties propreties2 = new Properties();
      String str2 = dataBasePrefs.get("connection.password", "");
      propreties2.put("connection.password", str2);
      
      Properties propreties3 = new Properties();
      String str3 = dataBasePrefs.get("connection.url", "");
      propreties3.put("connection.url", str3);
      
      
      configuration.addProperties(propreties1);
      configuration.addProperties(propreties2);
      configuration.addProperties(propreties3);
      
      SessionFactory sessionFactory = configuration.buildSessionFactory();
      HibernateUtil.setSessionFactory(sessionFactory);
      /* Fin initialisation */


But I hace this exception
Code:
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
   at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
   at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
   at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
   at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
   at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.beginTransaction(Unknown Source)
   at com.ullink.extranet.module.orderBookMonitoring.dao.OrderBook.DaoOrderBookImpl.getAllTblconfeventsync(DaoOrderBookImpl.java:746)
   at com.ullink.extranet.module.orderBookMonitoring.service.OrderBook.ServiceOrderBookImpl.getAllTblconfeventsync(ServiceOrderBookImpl.java:446)
   at com.ullink.extranet.module.orderBookMonitoring.web.Connection.MemberAreaFacesServlet$1.run(MemberAreaFacesServlet.java:46)
   at java.lang.Thread.run(Unknown Source)


The exception is launched when I try to start a transaction (the 2nd line)
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();


This is my hibernate.cfg.xml file :
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

   <session-factory>

      <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="connection.driver_class">org.postgresql.Driver</property>
      
      <!--<property name="connection.username">postgres</property>
      <property name="connection.password">postgres</property>
      <property name="connection.url">jdbc:postgresql://localhost/db_memberarea</property>-->
               
        <!-- Echo all executed SQL to stdout
        <property name="show_sql">true</property>-->
       
        <property name="hibernate.transaction.factory_class">
           org.hibernate.transaction.JDBCTransactionFactory
      </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

      <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
       
        <!-- <property name="hibernate.jdbc.batch_size">0</property>  -->
       
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
       
       
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblorder.hbm.xml" />
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tbltrade.hbm.xml" />
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblconfeventsync.hbm.xml" />

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


Do you have any idea about this exception ?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 5:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am doing something similar and I think all property keys need to be prefixed with "hibernate.". Eg.

Code:
propreties3.put("hibernate.connection.url", str3);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 5:27 am 
Newbie

Joined: Mon Jul 16, 2007 10:44 am
Posts: 15
Thanks nordborg for your reply,

I added the "hibernate." It's more better thanks :) . The exception disappear but unfortunately I have another one now:
Code:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:426)
   at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
   at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
   at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
   at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.beginTransaction(Unknown Source)
   at com.ullink.extranet.module.orderBookMonitoring.dao.OrderBook.DaoOrderBookImpl.getAllTblconfeventsync(DaoOrderBookImpl.java:746)
   at com.ullink.extranet.module.orderBookMonitoring.service.OrderBook.ServiceOrderBookImpl.getAllTblconfeventsync(ServiceOrderBookImpl.java:446)
   at com.ullink.extranet.module.orderBookMonitoring.web.Connection.MemberAreaFacesServlet$1.run(MemberAreaFacesServlet.java:46)
   at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
   at java.sql.DriverManager.getConnection(Unknown Source)
   at java.sql.DriverManager.getConnection(Unknown Source)
   at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)


any idea, thnkas !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 5:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I noted another difference between your code and mine. I make the call to configuration.configure() after all properties has been set on the configuration. Try moving this line to the line just before SessionFactory sessionFactory = configuration.buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 6:00 am 
Newbie

Joined: Mon Jul 16, 2007 10:44 am
Posts: 15
I did what you say, but it doesn't work !
Code:
Configuration configuration = new Configuration();
      
      
      /* Initialiser le fichier hibernate.cfg.xml */
      
      Preferences dataBasePrefs = prefs.node("database");
      
      Properties propreties1 = new Properties();
      String str1 = dataBasePrefs.get("hibernate.connection.username", "");
      propreties1.put("hibernate.connection.username", str1);
      
      Properties propreties2 = new Properties();
      String str2 = dataBasePrefs.get("hibernate.connection.password", "");
      propreties2.put("hibernate.connection.password", str2);
      
      Properties propreties3 = new Properties();
      String str3 = dataBasePrefs.get("hibernate.connection.url", "");
      propreties3.put("hibernate.connection.url", str3);
      
      
      configuration.addProperties(propreties1);
      configuration.addProperties(propreties2);
      configuration.addProperties(propreties3);
      
      configuration.configure();
      
      SessionFactory sessionFactory = configuration.buildSessionFactory();
      HibernateUtil.setSessionFactory(sessionFactory);


and my hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

   <session-factory>

      <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
      
      <!--<property name="connection.username">postgres</property>
      <property name="connection.password">postgres</property>
      <property name="connection.url">jdbc:postgresql://localhost/db_memberarea</property>-->
               
        <!-- Echo all executed SQL to stdout
        <property name="show_sql">true</property>-->
       
        <property name="hibernate.transaction.factory_class">
           org.hibernate.transaction.JDBCTransactionFactory
      </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

      <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
       
        <!-- <property name="hibernate.jdbc.batch_size">0</property>  -->
       
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
       
       
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblorder.hbm.xml" />
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tbltrade.hbm.xml" />
      <mapping resource="com/ullink/extranet/module/orderBookMonitoring/mapping/Tblconfeventsync.hbm.xml" />

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


Is there any differences between my hibernate.cfg.xml and yours ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 6:14 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In the hibernate.cfg.xml file I don't have the "hibernate." prefix on the properties. So:

Code:
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>


may help you and this is what you had in the first post. It may seem confusing but it seems like the prefix was "lost" when the newer XML configuration format was introduced.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 7:42 am 
Newbie

Joined: Mon Jul 16, 2007 10:44 am
Posts: 15
Yes, I added hibernate. in the propreties just to make test, but it doesn't work also ...

If you did the same things like me ! can post your hibernate.cfg.xml and the configuration program ? Maybe It will help me

ah, i don't if it came from this class !!!

Code:
public class HibernateUtil {
   //private static final SessionFactory sessionFactory;
   private static SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
           
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
   
    public static void setSessionFactory(SessionFactory session) {
        sessionFactory = session;
    }
   
}


thanks lot

edit : I tried to disable
sessionFactory = new Configuration().configure().buildSessionFactory();
but no change, I have always the same exception


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 8:15 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have no connection-related settings in the hibernate.cfg.xml file. I do everything in the code. Here it is:

Code:
private static void setConfigurationProperties(Configuration cfg)
   throws BaseException
{
   assert cfg != null : "cfg == null";
   cfg.setProperty("hibernate.dialect", Config.getString("db.dialect"));
   cfg.setProperty("hibernate.connection.driver_class", Config.getString("db.driver"));
   cfg.setProperty("hibernate.connection.url", Config.getString("db.url"));
   cfg.setProperty("hibernate.connection.username", Config.getString("db.username"));
   cfg.setProperty("hibernate.connection.password", Config.getString("db.password"));
}


Are you sure that the dataBasePrefs.get() method is returning the proper values? I can duplicate the "No suitable driver..." exception by setting "hibernate.connection.url" to an empty string.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 8:25 am 
Newbie

Joined: Mon Jul 16, 2007 10:44 am
Posts: 15
Hi,

I found the problem, I make a mistake her
Code:
String str1 = dataBasePrefs.get("hibernate.connection.username", "");

I forgot that in my prefs file I have no key like this. So when I change to
Code:
String str1 = dataBasePrefs.get("connection.username", "");

It work :)

So You were right a longtimes before :)

Thank you very much nordborg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.