hibernate: datasource

N

nubsi

Gast
ich versuche gerade eine hsql datenbank via hibernate + datasource anzubinden.

folgender code funktioniert zwar ist aber nicht optimal. da ich keine möglichkeit habe das ganze zeug via persistence.xml zu konfigurieren, mache ich es via java code:

Java:
final Ejb3Configuration configuration = new Ejb3Configuration();
final EntityManagerFactory entityManagerFactory;
final EntityManager manager;
final EntityTransaction transaction;

configuration.addAnnotatedClass(MyAnnotatedClass.class);
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
configuration.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
configuration.setProperty("hibernate.connection.username", "sa");
configuration.setProperty("hibernate.connection.password", "");
configuration.setProperty("hibernate.connection.url", "jdbc:hsqldb:mydatabase");
configuration.setProperty("hibernate.hbm2ddl.auto", "create");
configuration.setProperty("hibernate.max_fetch_depth", "3");
configuration.setProperty("hibernate.showSql", "true");

entityManagerFactory = configuration.createEntityManagerFactory();
manager = this.entityManagerFactory.createEntityManager();
transaction = manager.getTransaction();

transaction.begin();
try {
	manager.persist(new MyAnnotatedClass());
	transaction.commit();
} catch(Throwable t) {
	transaction.rollback();
}

um jetzt trotzdem ein wenig konfigurationsmöglichkeiten zu haben, wollte ich eine datasource verwenden:

${JBOSS_HOME}/server/default/deploy/mydatasource-ds.xml
[XML]<?xml version="1.0" encoding="UTF-8"?>

<datasources>
<local-tx-datasource>

<!-- The jndi name of the DataSource, it is prefixed with java:/ -->
<!-- Datasources are not available outside the virtual machine -->
<jndi-name>MyDatasourceDS</jndi-name>

<!-- For in-process persistent db, saved when jboss stops.
The org.jboss.jdbc.HypersonicDatabase mbean is required for proper db shutdown -->
<connection-url>jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}mydatabase</connection-url>

<!-- The driver class -->
<driver-class>org.hsqldb.jdbcDriver</driver-class>

<!-- The login and password -->
<user-name>sa</user-name>
<password></password>

<!-- The minimum connections in a pool/sub-pool. Pools are lazily constructed on first use -->
<min-pool-size>5</min-pool-size>

<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>20</max-pool-size>

<!-- The time before an unused connection is destroyed -->
<idle-timeout-minutes>0</idle-timeout-minutes>

<!-- Use the security domain defined in conf/login-config.xml -->
<security-domain>HsqlDbRealm</security-domain>

<!-- HSQL DB benefits from prepared statement caching -->
<prepared-statement-cache-size>32</prepared-statement-cache-size>

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>Hypersonic SQL</type-mapping>
</metadata>

<!-- When using in-process (standalone) mode -->
<depends>jboss:service=Hypersonic,database=mydatabase</depends>

</local-tx-datasource>

<!-- For hsqldb accessed from jboss only, in-process (standalone) mode -->
<mbean code="org.jboss.jdbc.HypersonicDatabase" name="jboss:service=Hypersonic,database=mydatabase">
<attribute name="Database">mydatabase</attribute>
<attribute name="InProcessMode">true</attribute>
</mbean>

</datasources>
[/XML]

Java:
final Ejb3Configuration configuration = new Ejb3Configuration();
final EntityManagerFactory entityManagerFactory;
final EntityManager manager;
final EntityTransaction transaction;

configuration.addAnnotatedClass(MyAnnotatedClass.class);
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
configuration.setProperty("connection.datasource", "java:MyDatasourceDS");

entityManagerFactory = configuration.createEntityManagerFactory();
manager = this.entityManagerFactory.createEntityManager();
transaction = manager.getTransaction();

transaction.begin();
try {
	manager.persist(new MyAnnotatedClass());
	transaction.commit();
} catch(Throwable t) {
	transaction.rollback();
}

und eben das funktioniert leider nicht. der aufruf von "transaction.begin()" erzeugt eine java.lang.UnsupportedOperationException:

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 org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
	...

zuvor ist im log noch das hier zu sehen:

Code:
[SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured

das ganze soll in einem jboss-4.2.3 laufen.
mache ich was falsch?
hab ich was vergessen/übersehen?
 

DanZ

Bekanntes Mitglied
Funktioniert es vielleicht mit "hibernate.connection.datasource" als Propertykey?
Ansonsten könntest du die Datasource auch direkt aus dem JNDI holen und mit .setDataSource in die Config schreiben.
Die Logmeldung bezieht sich auf die Sessionfactory und hat nichts mit dem Datasource Problem zu tun.
 
N

nubsi

Gast
sowohl die verwendung von "hibernate.connection.datasource" als property key als auch die methode setDataSource() sorgt nur für zusätzliche fehler.

Die Logmeldung bezieht sich auf die Sessionfactory und hat nichts mit dem Datasource Problem zu tun.

und was muß ich tun, um das problem zu lösen? fehlt etwas in der konfiguration? muß ich eine eigene session factory bauen/definieren?
 

DanZ

Bekanntes Mitglied
Die Meldung sagt nur, dass die SessionFactory nicht im JNDI abgelegt wird - du brauchst sie da ja auch garnicht, weil du sie ja über die EntityManagerFactory direkt im Code hast. Es sollte also kein Problem sein.

Dann mal eine andere Frage: In welchem Context ist der Code in dem du den EntityManager baust? Wie wird der aufgerufen, wo wird der ausgeführt?

Edit: und steht vielleicht im Log noch irgendetwas zur Datasource? Kann sein dass das ganz am Anfang ist.
 
Zuletzt bearbeitet:
N

nubsi

Gast
In welchem Context ist der Code in dem du den EntityManager baust? Wie wird der aufgerufen, wo wird der ausgeführt?

es handelt sich um eine art sub-modul innerhalb eines ejb containers, der seinerseits durch jboss ausgeführt und verwaltet wird.

steht vielleicht im Log noch irgendetwas zur Datasource? Kann sein dass das ganz am Anfang ist.

wenn du mit anfang meinst, ob das bei jboss start mitgeladen wird: ja.

beim aufruf von configuration.createEntityManagerFactory() wird unter anderen das hier ausgegeben:

Code:
...
WARN  [UserSuppliedConnectionProvider] No connection properties specified - the user must supply JDBC connections
...
INFO  [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured

kann es sein, das ich hier gar keine datasource verwenden kann, sondern stehts eine echte verbindung einstellen muß?
 
N

nubsi

Gast
nachtrag:

mit dem property "hibernate.connection.datasource" => "java:/MyDatasourceDS" scheine ich einerseits auf dem richtigen weg zu sein (zumindest wird nichts ausgegeben wie "datasource not found" oder so). dennoch werden einige exceptions geworfen:

Code:
INFO  [DatasourceConnectionProvider] Using datasource: java:/MyDatasourceDS
WARN  [JBossManagedConnectionPool] Throwable while attempting to get a new connection: null
org.jboss.resource.JBossResourceException: No matching credentials in Subject!
        at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory.getConnectionProperties(BaseWrapperManagedConnectionFactory.java:404)
        at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:149)
        at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.createConnectionEventListener(InternalManagedConnectionPool.java:619)
        at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.getConnection(InternalManagedConnectionPool.java:264)
        at org.jboss.resource.connectionmanager.JBossManagedConnectionPool$BasePool.getConnection(JBossManagedConnectionPool.java:575)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:347)
        at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:330)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:402)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:849)
        at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89)
        at org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:69)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
        at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:700)
        ...

WARN  [SettingsFactory] Could not obtain connection metadata
org.jboss.util.NestedSQLException: No matching credentials in Subject!; - nested throwable: (org.jboss.resource.JBossResourceException: No matching credentials in Subject!)
        at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95)
        at org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:69)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
        at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:700)
        ...
Caused by: org.jboss.resource.JBossResourceException: No matching credentials in Subject!
        at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory.getConnectionProperties(BaseWrapperManagedConnectionFactory.java:404)
        at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:149)
        at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.createConnectionEventListener(InternalManagedConnectionPool.java:619)
        at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.getConnection(InternalManagedConnectionPool.java:264)
        at org.jboss.resource.connectionmanager.JBossManagedConnectionPool$BasePool.getConnection(JBossManagedConnectionPool.java:575)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:347)
        at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:330)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:402)
        at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:849)
        at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89)
        ... 14 more
 
N

nubsi

Gast
ich werd irre... eben noch schnell ("hibernate.hbm2ddl.auto" => "create") hinzugefügt und schon funktioniert es.

besten dank :)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
torresbig MySQL hibernate - could not resolve entity class ... (Datenbank Anfänger) Datenbankprogrammierung 19
I Hibernate Predicate mit IN Clause "Unaware how to convert value to requested type" Datenbankprogrammierung 0
T org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session Datenbankprogrammierung 5
T Realisierungsvorschläge Hibernate SQL Datenbankprogrammierung 1
Zrebna Wie mittels Hibernate eine Join-Tabelle als eigene Java-Klasse erstellen? Datenbankprogrammierung 5
Zrebna Tabellen-Erzeugung via Hibernate ignoriert 'CascadeType' settings Datenbankprogrammierung 1
I Hibernate / JPA - Spaltenname von Query (Select) bekommen Datenbankprogrammierung 6
M Mysql Views und Hibernate Caching Datenbankprogrammierung 4
damike84 Hibernate: persistieren eines Graphen Datenbankprogrammierung 2
N Hibernate Entitäten bei Selects aus 2 Tabellen Datenbankprogrammierung 7
OnDemand Hibernate Realationen Datenbankprogrammierung 7
OnDemand Hibernate ManyToOne Datenbankprogrammierung 5
D Hibernate oneToMany Syntaxfehler Datenbankprogrammierung 3
D Hibernate Error: NoSuchMethodError Datenbankprogrammierung 4
D hibernate mit postgreSQL Datenbankprogrammierung 3
S MySQL Hibernate: Fehler bei Verwendung von 2 unterschiedlichen Enumration Datenbankprogrammierung 3
F Problem mit Hibernate c3p0 Datenbankprogrammierung 2
OnDemand MySQL Trigger löst nicht aus bei Hibernate Update Datenbankprogrammierung 12
OnDemand Hibernate OneToMany ManyToOne Datenbankprogrammierung 61
J Hibernate One-To-One mit Where Klausel Datenbankprogrammierung 6
L hibernate.cfg.xml Could not parse configuration Datenbankprogrammierung 0
L H2 Hibernate definieren? Datenbankprogrammierung 1
T JPA Mapping Enum (hibernate 5) Datenbankprogrammierung 1
H In hibernate.cfg.xml schreiben und auslesen Datenbankprogrammierung 0
K Hibernate: Ein Fluch Datenbankprogrammierung 3
K Java Object mit Hibernate in MySQL abspeichern Datenbankprogrammierung 1
K Eclipse: JBoss Hibernate Tool: Kein Zugriff auf Datenbank Datenbankprogrammierung 5
S JpaRepositories & Hibernate: ungewolltes trim() in findBy Datenbankprogrammierung 7
S MySQL hibernate exception: illegal state exception : entityManagerFactory is closed Datenbankprogrammierung 5
S Hibernate: Verschiedene Klassen zurückgeben. Datenbankprogrammierung 2
looparda Architektur für JPA Hibernate Umstieg Datenbankprogrammierung 14
O HSQLDB Hibernate Criteria Problem Datenbankprogrammierung 3
perlenfischer1984 Hibernate mit final members Datenbankprogrammierung 3
perlenfischer1984 Java Objecte speichern mit Hibernate ? Datenbankprogrammierung 2
N SQLite Hibernate und Aufruf von Funktion SELECT last_insert_rowid() Datenbankprogrammierung 2
N Sqlite3 und Hibernate Datenbankprogrammierung 3
A Hibernate Cache leeren Datenbankprogrammierung 4
I MySQL Hibernate zu viele Queries Datenbankprogrammierung 2
Psypsy Hibernate / JPA erkennen von schon gespeicherten Objekten Datenbankprogrammierung 4
Psypsy Hibernate / JPA OneToOne MappedBy Frage Datenbankprogrammierung 2
J Hibernate + DTOs - DTOs in DAOs verwenden? Datenbankprogrammierung 1
S Hibernate-Konfiguration : Unverständliche Ausgabe beim Ausführen Datenbankprogrammierung 0
I MySQL Hibernate / MySQL alias in WHERE clause Datenbankprogrammierung 1
J Hibernate + HSQL embedded Datenbankprogrammierung 2
P Hibernate Einstieg Datenbankprogrammierung 5
C Hibernate und createQuery Datenbankprogrammierung 2
V kennt jemand empfehlenswerte online tutorials zur Hibernate ? gerne auch englisch. Datenbankprogrammierung 4
G H2 Hibernate - wie joins machen Datenbankprogrammierung 1
D Hibernate: Zustand eines Objekts erkennen? Datenbankprogrammierung 0
D Unterschiede Hibernate Vs. Java Persistence API Datenbankprogrammierung 8
I Hibernate / JPA Index hinzufügen Datenbankprogrammierung 1
X Hibernate Cache Verständnisproblem Datenbankprogrammierung 0
T Hibernate und inner class Datenbankprogrammierung 0
K n:m Tabellen mit Hibernate erstellen Datenbankprogrammierung 1
T Hibernate DAO gute Tutorials/Bücher gesucht Datenbankprogrammierung 0
C Hibernate: could not resolve property Datenbankprogrammierung 1
J Plug-In-Framework für Hibernate-Klassen Datenbankprogrammierung 0
M Hibernate - Save Child wenn nötig Datenbankprogrammierung 10
M DAO's + Hibernate Theorie Datenbankprogrammierung 4
T Hibernate, HSQLDB und UNIQUE Datenbankprogrammierung 2
F Hibernate - verschiedene Schemen Datenbankprogrammierung 7
D Hibernate SaveOrUpdate Exception Datenbankprogrammierung 2
D Hibernate CreateQuery ohne Result Datenbankprogrammierung 7
E MySQL Hibernate mit JaxWS führt zu LazyInitialization Exception Datenbankprogrammierung 8
L Einarbeitung in Hibernate -> wenn gute SQL Kenntnisse vorhanden? Datenbankprogrammierung 2
B DB2 Hibernate findet Datenbank nicht Datenbankprogrammierung 18
K JPA / Hibernate Annotations Datenbankprogrammierung 4
M JPA / Hibernate mit Postgres DB Datenbankprogrammierung 3
P JSF + H2 + TomEE + Hibernate/JPA Datenbank wird nicht angelegt Datenbankprogrammierung 3
E MySQL Hibernate ( Anfänger ) Datenbankprogrammierung 3
P Lazy-Fetchig und Session-Problem mit Hibernate Datenbankprogrammierung 4
J Hibernate Select auf Parameterliste Datenbankprogrammierung 3
C Hibernate ManyToMany zusammengesetzter Primärschlüssel, problem. Datenbankprogrammierung 3
P Oracle Hibernate - Oracle-VarChar-Index wird nicht genutzt Datenbankprogrammierung 3
M Hibernate Foreign Key definieren Datenbankprogrammierung 4
M Abstrakte Klassen Hibernate Datenbankprogrammierung 4
D Mit Hibernate (mit Annotation) auf Views zugreifen Datenbankprogrammierung 2
M [Hibernate]Abgleich eines lokalen Objekts mit dem Zustand aus der Datenbank. Datenbankprogrammierung 3
P Mit Hibernate mehrere Datensätze löschen Datenbankprogrammierung 7
P Probleme mit meinem 1. Hibernate Beispiel Datenbankprogrammierung 3
P erste Schritte mit Hibernate Datenbankprogrammierung 3
V Hibernate Interfaces von anderem Projekt Datenbankprogrammierung 2
J MySQL Datenbank konfigurieren, JDBC, MySQL oder Hibernate Datenbankprogrammierung 2
B Hibernate und portierbare Datenbank Datenbankprogrammierung 3
qwerqer [Hibernate] Mappingvarianten Datenbankprogrammierung 2
lumo Teneo Hibernate & JPA Datenbankprogrammierung 15
Z JPA mit Hibernate - Unable to build EntityManagerFactory Datenbankprogrammierung 7
Dit_ Hibernate, effiziente SQL-Abfrage definieren Datenbankprogrammierung 5
K Hibernate vs. JDBC Datenbankprogrammierung 4
J Hibernate Info 593 ? Datenbankprogrammierung 4
J Erstellen der SessionFactory in Hibernate 4.1 Datenbankprogrammierung 2
L PostgreSQL Hibernate-Frage Datenbankprogrammierung 2
X MySQL Hibernate: Massenupdate auf unbekannte Tabelle Datenbankprogrammierung 4
H MySQL Hibernate: Updaten vereinfachen Datenbankprogrammierung 2
T Hibernate Division zweier Summen Datenbankprogrammierung 4
B MySQL Fehler: Cannot open connection mit Tomcat7, Hibernate und MySQL Datenbankprogrammierung 4
F GWT und Hibernate - gwt.dev.Compler not found Datenbankprogrammierung 3
M NestedTransaction- Exception in Hibernate Datenbankprogrammierung 15
O Löschen vieler Datensätze mit Hibernate Datenbankprogrammierung 11
C Hibernate n:m mittels Zwischentabelle und bidirektionaler Zugriff Datenbankprogrammierung 2

Ähnliche Java Themen

Neue Themen


Oben