Попытка создать схему базы данных - не было доступного поставщика БД, не удалось создать соединение - PullRequest
3 голосов
/ 16 июня 2011

Я начал с примера Northwind spring.net/NHibernate. Я пытаюсь получить существующий пример для создания схемы. Я изменил запись CustomerEditController web.xml на

<object name="CustomerEditController" type="NHibernateCustomerEditController" scope="session">
  <constructor-arg name="sessionFactory" ref="NHibernateSessionFactory"/>
  <constructor-arg name="local" ref="&amp;NHibernateSessionFactory"/>
</object>`

Изменено NHibernateCustomerEditController на следующее:

public class NHibernateCustomerEditController : ICustomerEditController
{
    private readonly ISessionFactory sessionFactory;
    private readonly LocalSessionFactoryObject LocalsessionFactory;
    private Customer currentCustomer;

    public NHibernateCustomerEditController(ISessionFactory sessionFactory, LocalSessionFactoryObject local)
    {
        this.sessionFactory = sessionFactory;
        this.LocalsessionFactory = local;
    }

    private ISession Session
    {
        get { return sessionFactory.GetCurrentSession(); }
    }

    public void EditCustomer(Customer customer)
    {
        currentCustomer = customer;
    }

    public void Clear()
    {
        currentCustomer = null;
    }

    public Customer CurrentCustomer
    {
        get
        {
            Customer customer = currentCustomer;

            //since the Customer entity may have been retrieved from a prior request, we need to reattach it to the current session
            // in order to support lazy-loading, etc. on the Customer
            Session.Lock(customer, LockMode.None);

            return customer;
        }
    }
    public void MakeANewDatabase() {
        SchemaExport schema = new SchemaExport(LocalsessionFactory.Configuration);
        schema.Create(true, true);
    }

}

Я добавил кнопку на страницу списка клиентов, которая ведет к методу MakeANewDatabase.

Когда я нажимаю кнопку, я получаю сообщение об ошибке There was no DB provider available, unable to create connection. Похоже, что когда SchemaExport создается, DBProvider является нулевым.

Полный текст ошибки:

An exception of type 'System.Exception' occurred in Spring.Data.NHibernate30.DLL but was not handled in user code

Additional information: There was no DB provider available, unable to create connection

An exception of type 'NHibernate.HibernateException' occurred in NHibernate.DLL but was not handled in user code

Additional information: There was no DB provider available, unable to create connection

1 Ответ

3 голосов
/ 20 июня 2011

Похоже, что конфиг, извлеченный из локальной фабрики сессий, заполнен не полностью. Решил проблему с помощью методов Spring.

public void MakeANewDatabase()
{ 
  LocalsessionFactory.CreateDatabaseSchema(); 
}
...