Using Postgres with Grails - postgresql

Has anyone gotten Grails working with Postgres? I have used this tutorial and everything seems to make sense and be correct to me. However when I 'grails run-app' I get this error
Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL 'jdbc:postgres://10.0.0.21/tribes'
java.sql.SQLException: No suitable driver
My DataSource file is
dataSource {
pooled = true
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update"
url = "jdbc:postgres://10.0.0.21:5432/tribes"
username = "grails"
password = "grails"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:postgres://10.0.0.21:5432/tribes"
username = "grails"
password = "grails"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:postgres://10.0.0.21:5432/tribes"
username = "grails"
password = "grails"
}
}
}

From the FAQ: "[if] you get a runtime error that says 'No suitable driver found', it is likely that the URL passed to DriverManager.getConnection is malformed or otherwise incorrect". So what's wrong with yours? Well, the examples in the tutorial look like this:
jdbc:postgresql://localhost:5432/grails
Yours looks like this:
jdbc:postgres://10.0.0.21:5432/tribes
I'm guessing those missing two letters are causing your trouble.

In the BuildConfig.groovy file uncomment the external maven repositories and then add this line
runtime 'postgresql:postgresql:9.0-801.jdbc4' in the dependencies section

Related

how to form url in prisma.schema file using system variable

generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgresql://${app_db_username}:${app_db_password}#${endpoint}/${dbname}?schema=public"
}
I'm trying to form the below url, so how to use it
String concatenation is not implemented in Prisma yet. As of now you need to provide an environment variable that contains full database url:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
You can use an .env file to prepare the environment variable (Prisma v2.7.0+):
# .env
DATABASE_URL=postgresql://${app_db_username}:${app_db_password}#${endpoint}/${dbname}?schema=public

Authenticating while connecting to multiple database using mongoose

I have set up auth in my MongoDB as mentioned here. Initially, in my project I was accessing a single database say, firstdb from mongoose using
let url = "mongodb://localhost:27017/firstdb";
let options = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username1",
pass:"mypassword1"
};
mongoose.connect(url,options,callback);
The user with username and mypassword was created in the firstdb itself giving it readWrite perms. I did this while logged in with my admin user.
Things were working smoothly. Then I had a requirement of connecting to a second database. So I changed my code as such
let url1 = "mongodb://localhost:27017/firstdb";
let options1 = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username1",
pass:"mypassword1",
auth:{
authdb:"firstdb"
}
};
let connection1 = mongoose.createConnection(url1,options1);
let url2 = "mongodb://localhost:27017/seconddb";
let options2 = {
server:{
socketOptions:{
keepAlive:120
}
},
user:"username2",
pass:"mypassword2",
auth:{
authdb:"seconddb"
}
};
let connection2 = mongoose.createConnection(url2,options2);
This time I created user username2 the same way in the seconddb database. But now mongoose is unable to perform any operation and is failing with Not authorized to execute command. I can access the db through mongo shell though. I also spun up the code in my local system which doesn't have mongodb auth enabled and it works fine there. Please help
So, after shooting around in the blind, found something that works.
Use the username and password in the url itself like
let connection1 = mongoose.createConnection("mongodb://username1:password1#localhost:27017/firstdb");
Still, I would like to know why passing the parameters as options doesn't work.

Codeigniter - Form validation callback not working on server, but OK on WAMP

I've developed a CI site on my local machine using WAMP. I'm using CI 3 with the HMVC extension and it all works fine.
I've just uploaded the site to the production server and changed the config files etc to get it working. However, form validation callbacks are not working on the production server.
Here's an example from a login form:
// Process login form
public function submit()
{
$this->load->library('form_validation');
$username = $this->input->post('username', TRUE);
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_do_login');
if($this->form_validation->run($this) == FALSE)
{
$this->login();
}
else
{
// Set login session
...
}
}
This is the callback function do_login:
public function do_login($password)
{
// Get user / pass from POST
$submitted_password = $this->input->post('password', TRUE);
$username = $this->input->post('username', TRUE);
$this->crud->setTable($this->table);
$this->load->model('mdl_users');
// Check User Exists
$query = $this->mdl_users->getUser($username);
if($query->num_rows() == 1)
{
// Get stored password
$row = $query->row();
$stored_password = $row->password;
// Check password
$this->load->module('mod_security');
$result = $this->mod_security->login($username, $submitted_password, $stored_password); // Returns false if no match
return ($result) ? TRUE : FALSE;
}
else
{
return FALSE;
}
}
On my local WAMP setup, this all works fine. But on the server
$this->form_validation->run($this)
always returns false.
To eliminate any errors with the callback function itself, I changed it to the following as a test:
public function do_login($password)
{
return TRUE;
}
... which will obviously always return TRUE, however when $this->form_validation->run($this) is called, even though I changed the do_login callback function to return TRUE, $this->form_validation->run($this) still returns FALSE!!
This is driving me crazy and I have no idea why this is happening. It is as if the form validation is ignoring the callback function and just returning false.
Can anyone help me? Could it be a setting on the server causing it or something else that could have changed when I uploaded the site files to the server?
If it is relevant, on my local machine, within the do_login callback function I had originally set the callback error message like this:
$this->form_validation->set_message('do_login', 'User / Pass Incorrect');
...which worked fine, but on the production server it threw an error stating: "Unable to access an error message corresponding to your field name Password.(do_login)". I had to set the error message in the language library file to overcome this. But the fact that this happened on the production server and not on my WAMP setup makes me think there must be some setting or something on the server that is causing this.
Anyway, any help is gratefully received.
Thanks
I finally found the solution. The HMVC application requires a small library file named as MY_Form_validation.php. Seemingly everywhere on the net this file is shown to be thus:
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = '')
{
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
However, although this worked in my WAMP setup, it did not work on my server. I found that by declaring the CI variable at the start the server problem is resolved. So the MY_Form_validation.php file finally becomes:
class MY_Form_validation extends CI_Form_validation {
public $CI;
function run($module = '', $group = '')
{
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}

How to use #MailSessionDefinition in Wildfly

I am trying to use the #MailSessionDefinition to configure a MailSender in Wildfly 8.2. Using just the annotation was not enough to setup, I had to use specific xml, present in Wildfly. Tips?
Here is my code:
#MailSessionDefinition(name = "java:jboss/mail/gmail",
host = "smtp.gmail.com",
user="user",
password="password",
transportProtocol = "smtps",
from="test#gmail.com",
properties = {
"mail.debug=true",
"mail.smtp.port=587",
"mail.smtp.auth=true",
"mail.smtp.starttls.enable=true"
})
public class MailConfiguration {
}

having multiple sender in grails

I need to create a app for people which can send email, from anyone to anyone
I have tried using mail plugin in grails
mail {
host = "smtp.gmail.com"
port = 465
username = "adityasoni051293#gmail.com"
password = "aditya051293"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
how ever I am not able to change the sender from application,
I have tried the following code in my controller
def defaultFrom = grailsApplication.config.grails.mail.default.from
String oldUsername = grailsApplication.config.grails.mail.mailSender.username
String oldPassword = grailsApplication.config.grails.mail.mailSender.password
// Change the properties here; send the email
try {
grailsApplication.config.grails.mail.default.from = "${parent_personal_data.email}"
grailsApplication.config.grails.mail.mailSender.username = "${parent_personal_data.email}"
grailsApplication.config.grails.mail.mailSender.password = "${parent_data.password}"
sendMail {
to "${employee_personal_data.email}"
subject "new task"
body "you have been added to project and you are given a task"
}
}
catch (Exception e) {
// catch block code
}
// Set the original settings back
finally {
grailsApplication.config.grails.mail.default.from = defaultFrom
grailsApplication.config.grails.mail.mailSender.username = oldUsername
grailsApplication.config.grails.mail.mailSender.password = oldPassword
but it is also using the id i set in config.groovy.
is there any way out.
or any other other plugin that I can use
please help I am waiting..... thanks
The problem you are facing is the fact the Grails Mail plugin is not designed to have it's host/port/username/password/connection properties changed at runtime. It's designed to work with one setting defined in Config.groovy.
If you need to be able to set that information at runtime then you will have to use Java Mail directly. Fortunately, you can look at the source code for the Grails Mail plugin to give you some ideas and even leverage the Spring Framework (Grails is built on Spring) for sending mail.
I had a similiar problem.
You can pass config to MailService#sendMail method.
`MailMessage sendMail(Config config, #DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = MailMessageBuilder) Closure callable)`
Here is an example: https://github.com/rgorzkowski/grails-multiple-mail-senders/blob/master/grails-app/services/pl/stepwise/EmailNotificationService.groovy
and source code https://github.com/rgorzkowski/grails-multiple-mail-senders