Hallo,
ich habe ein Problem mit der Konfiguration von meinem (ersten) hibernate-Projekt. Queries sind extrem langsam: simple Query, die etwa 300 Datensätze liefern sollte dauert bis zu 30s. Das aber nur, wenn ich eine Datenbank anspreche, die auf einem Webserver läuft. Anfragen an mein localhost System werden absolut normal abgearbeitet. "Normale" SQL anfragen an den Server werden schnell abgearbeitet, daher vermute ich, dass ein Konfigurationsfehler vorliegt.
Ich verwende einen ConnectionProvider. Die Klasse sieht folgendermaßen aus:
Code:
public class POConnectionProvider implements ConnectionProvider {
private MysqlDataSource ds = null;
private Properties props = null;
public void configure(Properties p) throws HibernateException {
props = p;
}
public Connection getConnection() throws SQLException {
Connection con = null;
try{
con = getPoolingDataSource().getConnection();
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
public Connection getJDBCConn() throws SQLException {
return getPoolingDataSource().getConnection();
}
public void closeConnection(Connection conn) throws SQLException {
conn.close();
}
public void close() throws HibernateException {
}
public boolean supportsAggressiveRelease() {
return false;
}
private MysqlDataSource getPoolingDataSource() {
if (ds == null) {
String username = props.getProperty("hibernate.connection.username");
String pass = props.getProperty("hibernate.connection.password");
ds = new MysqlDataSource();
ds.setServerName(Constants.getServerName());
ds.setDatabaseName(Constants.getDataBaseName());
ds.setPort(Constants.getConnectionPort());
ds.setUser(username);
ds.setPassword(pass);
return ds;
}
}
Und eine Klasse HibernateUtil, die die einzelnen Sessions liefern soll:
Code:
public class HibernateUtil {
private static SessionFactory factory = null;
private static ThreadLocal session = new ThreadLocal();
public static void createSession(String login, String pass)
throws ExceptionInInitializerError {
try {
Configuration cfg = new Configuration().configure("/projekt/pfad/hibernate.cfg.xml");
cfg.setProperty("hibernate.connection.username",login);
cfg.setProperty("hibernate.connection.password",pass);
cfg.setProperty(Environment.CONNECTION_PROVIDER, "projekt.pfad.POConnectionProvider");
POConnectionProvider cp = new POConnectionProvider();
factory = cfg.buildSessionFactory();
s.close();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
public static Session currentSession() {
Session s = (Session) session.get();
if (s == null) {
s = factory.openSession();
session.set(s);
}
return s;
}
public static Session openSession() {
return factory.openSession();
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
Die hibernate.cfg.xml:
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
--hier sind dann noch die Mappings
</session-factory>
</hibernate-configuration>
Initialisieren usw. ist alles kein Problem, nur die Geschwindigkeit der Abfragen ist wie gesagt zu langsam.
Unten habe ich noch die Auszüge vom log4j system mitgepostet.
Da ich mit meinem Latein am Ende bin (ich versuche jetzt schon seit zwei Monaten das Problem in den Griff zu kriegen), hoffe ich sehr, dass mir irgendjemand hier helfen kann.
Vielen Dank im Voraus!
Hibernate version: 3.1.3 Name and version of the database you are using: MySQL 4.1The generated SQL (show_sql=true):Auszüge vom log4j SystemCode:
11:22:41,578 INFO Environment:479 - Hibernate 3.1.3
11:22:41,578 INFO Environment:509 - hibernate.properties not found
11:22:41,578 INFO Environment:525 - using CGLIB reflection optimizer
11:22:41,593 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
//Hier die Mappings, die keine Probleme bereiten
11:22:42,593 INFO ConnectionProviderFactory:72 - Initializing connection provider: .path.POConnectionProvider
11:22:43,140 INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.1.10a
11:22:43,140 INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.12 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
11:22:43,156 INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLDialect
11:22:43,171 INFO TransactionFactoryFactory:34 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
11:22:43,187 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
11:22:43,187 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
11:22:43,187 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
11:22:43,187 INFO SettingsFactory:136 - JDBC batch size: 15
11:22:43,187 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
11:22:43,187 INFO SettingsFactory:144 - Scrollable result sets: enabled
11:22:43,187 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
11:22:43,187 INFO SettingsFactory:160 - Connection release mode: auto
11:22:43,187 INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
11:22:43,187 INFO SettingsFactory:187 - Default batch fetch size: 1
11:22:43,187 INFO SettingsFactory:191 - Generate SQL with comments: disabled
11:22:43,187 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
11:22:43,187 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
11:22:43,187 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
11:22:43,187 INFO SettingsFactory:203 - Query language substitutions: {}
11:22:43,187 INFO SettingsFactory:209 - Second-level cache: enabled
11:22:43,187 INFO SettingsFactory:213 - Query cache: disabled
11:22:43,203 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
11:22:43,203 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
11:22:43,203 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
11:22:43,218 INFO SettingsFactory:264 - Statistics: disabled
11:22:43,218 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
11:22:43,218 INFO SettingsFactory:283 - Default entity-mode: pojo
11:22:43,265 INFO SessionFactoryImpl:154 - building session factory
11:22:43,265 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/Pfad/ehcache-1.1.jar!/ehcache-failsafe.xml
11:22:44,031 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured